SlideShare a Scribd company logo
Introduction to
Graph Theory
Kaliningrad, Russia
Graph Theory - History
Leonhard Euler's paper on
“Seven Bridges of Königsberg”
(Kaliningrad ) ,
published in 1736.
Famous problems
• “The traveling salesman problem”
– A traveling salesman is to visit a number of cities; how to plan
the trip so every city is visited once and just once and the
whole trip is as short as possible ?
• In 1852 Francis Guthrie posed the “four color problem” which
asks if it is possible to color, using only four colors, any map of
countries in such a way as to prevent two bordering countries
from having the same color.
• This problem, which was only solved a century later in 1976 by
Kenneth Appel and Wolfgang Haken, can be considered the
birth of graph theory.
Examples
• Cost of wiring electronic components
• Shortest route between two cities.
• Shortest distance between all pairs of cities in a road
atlas.
• Matching / Resource Allocation
• Task scheduling
• Visibility / Coverage
Examples
• Flow of material
– liquid flowing through pipes
– current through electrical networks
– information through communication networks
– parts through an assembly line
• In Operating systems to model resource handling
(deadlock problems)
• In compilers for parsing and optimizing the code.
Basics
What is a Graph?
• Informally a graph is a set of nodes joined by a
set of lines or arrows.
1 1
2 3
4 4
5 5
6 6
2 3
Definition: Graph
• G is an ordered triple G:=(V, E, f)
– V is a set of nodes, points, or vertices.
– E is a set, whose elements are known as edges or
lines.
– f is a function
• maps each element of E
• to an unordered pair of vertices in V.
Definitions
• Vertex
– Basic Element
– Drawn as a node or a dot.
– Vertex set of G is usually denoted by V(G), or V
• Edge
– A set of two elements
– Drawn as a line connecting two vertices, called
end vertices, or endpoints.
– The edge set of G is usually denoted by E(G), or E.
Example
• V:={1,2,3,4,5,6}
• E:={{1,2},{1,5},{2,3},{2,5},{3,4},{4,5},{4,6}}
Kaliningrad
12
Introduction to graph theory
Edge types:
Undirected;
 E.g., distance between two cities, PPIs,
friendships…
Directed; ordered pairs of nodes.
 E.g. metabolic reactions, transcriptional
regulation,…
Loops; usually we assume no loops.
London Paris
London Paris
Path
• A path is a sequence of vertices such that there is an
edge from each vertex to its successor.
• A path is simple if each vertex is distinct.
1 2 3
4 5 6
Cycle
Simple path from 1 to 5
= [ 1, 2, 4, 5 ]
Our text’s alternates the vertices
and edges.
A
D E F
B C
Unreachable
Cycle
If there is path p from u to v then we
say v is reachable from u via p.
Cycle
• A path from a vertex to itself is called a cycle.
• A graph is called cyclic if it contains a cycle;
– otherwise it is called acyclic
1 2 3
4 5 6
Cycle
A
D E F
B C
Unreachable
Cycle
Connectivity
• is connected if
– you can get from any node to any other by following a
sequence of edges OR
– any two nodes are connected by a path.
• A directed graph is strongly connected if there is a
directed path from any node to any other node.
Sparse/Dense
• A graph is sparse if | E |  | V |
• A graph is dense if | E |  | V |2.
A weighted graph
• is a graph for which each edge has an associated
weight, usually given by a weight function w: E  R.
1 2 3
4 5 6
.5
1.2
.2
.5
1.5
.3
1
4 5 6
2 3
2
1
3
5
Special Types
• Empty Graph / Edgeless graph
– No edge
• Null graph
– No nodes
– Obviously no edge
Complete Graph
• Denoted Kn
• Every pair of vertices are adjacent
• Has n(n-1) edges
Planar Graph
• Can be drawn on a plane such that no two edges intersect
• K4 is the largest complete graph that is planar
Degree
• Number of edges incident on a node
A
D E F
B C
The degree of B is 2.
Degree (Directed Graphs)
• In degree: Number of edges entering
• Out degree: Number of edges leaving
• Degree = indegree + outdegree
1 2
4 5
The in degree of 2 is 2 and
the out degree of 2 is 3.
Subgraph
• Vertex and edge sets are subsets of those of G
– a supergraph of a graph G is a graph that contains
G as a subgraph.
Spanning subgraph
• Subgraph H has the same vertex set as G.
– Possibly not all the edges
– “H spans G”.
25
25
 Minimum spanning tree. Given a connected graph G = (V, E) with real-
valued edge weights (costs) ce, an MST is a subset of the edges T  E
such that T is a spanning tree (contains all nodes of G) whose sum of
edge weights is minimized.
 Cayley's Theorem. There are nn-2
spanning trees of Kn.
5
23
10
21
14
24
16
6
4
18
9
7
11
8
5
6
4
9
7
11
8
G = (V, E) T, eT ce = 50
can't solve by brute force
Minimum spanning tree
Representation (Matrix)
• Incidence Matrix
– E x V
– [edge, vertex] contains the edge's data
• Adjacency Matrix
– V x V
– Boolean values (adjacent or not)
– Or Edge Weights
Representation (List)
• Edge List
– pairs (ordered if directed) of vertices
– Optionally weight and other data
• Adjacency List
Implementation of a Graph.
• Adjacency-list representation
– an array of |V | lists, one for each vertex in V.
– For each u  V , ADJ [ u ] points to all its adjacent
vertices.
Adjacency-list representation for a directed
graph.
1
5
1
2
2
5
4 4
3 3
2 5
5 3 4
4
5
5
Variation: Can keep a second list of edges coming into a vertex.
Adjacency lists
• Advantage:
– Saves space for sparse graphs. Most graphs are
sparse.
– Traverse all the edges that start at v, in
(degree(v))
• Disadvantage:
– Check for existence of an edge (v, u) in worst
case time (degree(v))
Adjacency List
• Storage
– For a directed graph the number of items are
(out-degree (v)) = | E |
So we need ( V + E )
– For undirected graph the number of items are
(degree (v)) = 2 | E |
Also ( V + E )
• Easy to modify to handle weighted graphs. How?
v  V
v  V
Adjacency matrix representation
• |V | x |V | matrix A = ( aij ) such that
aij = 1 if (i, j ) E and 0 otherwise.
We arbitrarily uniquely assign the numbers 1,
2, . . . , | V | to each vertex.
1
5
2
4
3
1 2 3 4 5
1
2
3
4
5
0 1 0 0 1
1 0 1 1 1
0 1 0 1 0
0 1 1 0 1
1 1 0 1 0
Adjacency Matrix Representation for a
Directed Graph
1 2 3 4 5
1
2
3
4
5
0 1 0 0 1
0 0 1 1 1
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0
1
5
2
4
3
Adjacency Matrix Representation
• Advantage:
– Saves space for:
• Dense graphs.
• Small unweighted graphs using 1 bit per edge.
– Check for existence of an edge in (1)
• Disadvantage:
– Traverse all the edges that start at v, in (|V|)
Adjacency Matrix Representation
• Storage
– ( | V |2
) ( We usually just write, ( V 2
) )
– For undirected graphs you can save storage (only
1/2(V2
)) by noticing the adjacency matrix of an
undirected graph is symmetric. How?
• Easy to handle weighted graphs. How?

More Related Content

Similar to Lecture 4- Design Analysis Of ALgorithms (20)

PPTX
Graph Data Structure on social media analysis
raharjawahyuaguskade
 
PPTX
graph.pptx
DEEPAK948083
 
PDF
Graphs
Dwight Sabio
 
PPTX
Unit 2: All
Hector Zenil
 
PPTX
Graph in data structure
Pooja Bhojwani
 
PPTX
DATA STRUCTURES unit 4.pptx
ShivamKrPathak
 
PPTX
Graph Representation, DFS and BFS Presentation.pptx
bashirabdullah789
 
PPT
Chapter 5 Graphs (1).ppt
ishan743441
 
PPT
lec 09-graphs-bfs-dfs.ppt
TalhaFarooqui12
 
PDF
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
PPT
Graphs
LavanyaJ28
 
PDF
Daa chpater 12
B.Kirron Reddi
 
PPT
GT L7 graph.ppt Graph Theory - Discrete Mathematics
ThinnerBreath
 
PDF
09_DS_MCA_Graphs.pdf
Prasanna David
 
PPT
UNIT III discrete mathematice notes availiable
CHHAYANAYAK5
 
PPTX
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
PPT
GRAPHS notes presentation of non linear data structure
rasinenicharishma24
 
PPTX
Graphs in data structure
hamza javed
 
Graph Data Structure on social media analysis
raharjawahyuaguskade
 
graph.pptx
DEEPAK948083
 
Graphs
Dwight Sabio
 
Unit 2: All
Hector Zenil
 
Graph in data structure
Pooja Bhojwani
 
DATA STRUCTURES unit 4.pptx
ShivamKrPathak
 
Graph Representation, DFS and BFS Presentation.pptx
bashirabdullah789
 
Chapter 5 Graphs (1).ppt
ishan743441
 
lec 09-graphs-bfs-dfs.ppt
TalhaFarooqui12
 
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Graphs
LavanyaJ28
 
Daa chpater 12
B.Kirron Reddi
 
GT L7 graph.ppt Graph Theory - Discrete Mathematics
ThinnerBreath
 
09_DS_MCA_Graphs.pdf
Prasanna David
 
UNIT III discrete mathematice notes availiable
CHHAYANAYAK5
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
GRAPHS notes presentation of non linear data structure
rasinenicharishma24
 
Graphs in data structure
hamza javed
 

Recently uploaded (20)

PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Ad

Lecture 4- Design Analysis Of ALgorithms

  • 3. Graph Theory - History Leonhard Euler's paper on “Seven Bridges of Königsberg” (Kaliningrad ) , published in 1736.
  • 4. Famous problems • “The traveling salesman problem” – A traveling salesman is to visit a number of cities; how to plan the trip so every city is visited once and just once and the whole trip is as short as possible ? • In 1852 Francis Guthrie posed the “four color problem” which asks if it is possible to color, using only four colors, any map of countries in such a way as to prevent two bordering countries from having the same color. • This problem, which was only solved a century later in 1976 by Kenneth Appel and Wolfgang Haken, can be considered the birth of graph theory.
  • 5. Examples • Cost of wiring electronic components • Shortest route between two cities. • Shortest distance between all pairs of cities in a road atlas. • Matching / Resource Allocation • Task scheduling • Visibility / Coverage
  • 6. Examples • Flow of material – liquid flowing through pipes – current through electrical networks – information through communication networks – parts through an assembly line • In Operating systems to model resource handling (deadlock problems) • In compilers for parsing and optimizing the code.
  • 8. What is a Graph? • Informally a graph is a set of nodes joined by a set of lines or arrows. 1 1 2 3 4 4 5 5 6 6 2 3
  • 9. Definition: Graph • G is an ordered triple G:=(V, E, f) – V is a set of nodes, points, or vertices. – E is a set, whose elements are known as edges or lines. – f is a function • maps each element of E • to an unordered pair of vertices in V.
  • 10. Definitions • Vertex – Basic Element – Drawn as a node or a dot. – Vertex set of G is usually denoted by V(G), or V • Edge – A set of two elements – Drawn as a line connecting two vertices, called end vertices, or endpoints. – The edge set of G is usually denoted by E(G), or E.
  • 12. 12 Introduction to graph theory Edge types: Undirected;  E.g., distance between two cities, PPIs, friendships… Directed; ordered pairs of nodes.  E.g. metabolic reactions, transcriptional regulation,… Loops; usually we assume no loops. London Paris London Paris
  • 13. Path • A path is a sequence of vertices such that there is an edge from each vertex to its successor. • A path is simple if each vertex is distinct. 1 2 3 4 5 6 Cycle Simple path from 1 to 5 = [ 1, 2, 4, 5 ] Our text’s alternates the vertices and edges. A D E F B C Unreachable Cycle If there is path p from u to v then we say v is reachable from u via p.
  • 14. Cycle • A path from a vertex to itself is called a cycle. • A graph is called cyclic if it contains a cycle; – otherwise it is called acyclic 1 2 3 4 5 6 Cycle A D E F B C Unreachable Cycle
  • 15. Connectivity • is connected if – you can get from any node to any other by following a sequence of edges OR – any two nodes are connected by a path. • A directed graph is strongly connected if there is a directed path from any node to any other node.
  • 16. Sparse/Dense • A graph is sparse if | E |  | V | • A graph is dense if | E |  | V |2.
  • 17. A weighted graph • is a graph for which each edge has an associated weight, usually given by a weight function w: E  R. 1 2 3 4 5 6 .5 1.2 .2 .5 1.5 .3 1 4 5 6 2 3 2 1 3 5
  • 18. Special Types • Empty Graph / Edgeless graph – No edge • Null graph – No nodes – Obviously no edge
  • 19. Complete Graph • Denoted Kn • Every pair of vertices are adjacent • Has n(n-1) edges
  • 20. Planar Graph • Can be drawn on a plane such that no two edges intersect • K4 is the largest complete graph that is planar
  • 21. Degree • Number of edges incident on a node A D E F B C The degree of B is 2.
  • 22. Degree (Directed Graphs) • In degree: Number of edges entering • Out degree: Number of edges leaving • Degree = indegree + outdegree 1 2 4 5 The in degree of 2 is 2 and the out degree of 2 is 3.
  • 23. Subgraph • Vertex and edge sets are subsets of those of G – a supergraph of a graph G is a graph that contains G as a subgraph.
  • 24. Spanning subgraph • Subgraph H has the same vertex set as G. – Possibly not all the edges – “H spans G”.
  • 25. 25 25  Minimum spanning tree. Given a connected graph G = (V, E) with real- valued edge weights (costs) ce, an MST is a subset of the edges T  E such that T is a spanning tree (contains all nodes of G) whose sum of edge weights is minimized.  Cayley's Theorem. There are nn-2 spanning trees of Kn. 5 23 10 21 14 24 16 6 4 18 9 7 11 8 5 6 4 9 7 11 8 G = (V, E) T, eT ce = 50 can't solve by brute force Minimum spanning tree
  • 26. Representation (Matrix) • Incidence Matrix – E x V – [edge, vertex] contains the edge's data • Adjacency Matrix – V x V – Boolean values (adjacent or not) – Or Edge Weights
  • 27. Representation (List) • Edge List – pairs (ordered if directed) of vertices – Optionally weight and other data • Adjacency List
  • 28. Implementation of a Graph. • Adjacency-list representation – an array of |V | lists, one for each vertex in V. – For each u  V , ADJ [ u ] points to all its adjacent vertices.
  • 29. Adjacency-list representation for a directed graph. 1 5 1 2 2 5 4 4 3 3 2 5 5 3 4 4 5 5 Variation: Can keep a second list of edges coming into a vertex.
  • 30. Adjacency lists • Advantage: – Saves space for sparse graphs. Most graphs are sparse. – Traverse all the edges that start at v, in (degree(v)) • Disadvantage: – Check for existence of an edge (v, u) in worst case time (degree(v))
  • 31. Adjacency List • Storage – For a directed graph the number of items are (out-degree (v)) = | E | So we need ( V + E ) – For undirected graph the number of items are (degree (v)) = 2 | E | Also ( V + E ) • Easy to modify to handle weighted graphs. How? v  V v  V
  • 32. Adjacency matrix representation • |V | x |V | matrix A = ( aij ) such that aij = 1 if (i, j ) E and 0 otherwise. We arbitrarily uniquely assign the numbers 1, 2, . . . , | V | to each vertex. 1 5 2 4 3 1 2 3 4 5 1 2 3 4 5 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0
  • 33. Adjacency Matrix Representation for a Directed Graph 1 2 3 4 5 1 2 3 4 5 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 5 2 4 3
  • 34. Adjacency Matrix Representation • Advantage: – Saves space for: • Dense graphs. • Small unweighted graphs using 1 bit per edge. – Check for existence of an edge in (1) • Disadvantage: – Traverse all the edges that start at v, in (|V|)
  • 35. Adjacency Matrix Representation • Storage – ( | V |2 ) ( We usually just write, ( V 2 ) ) – For undirected graphs you can save storage (only 1/2(V2 )) by noticing the adjacency matrix of an undirected graph is symmetric. How? • Easy to handle weighted graphs. How?