Graphs in Data Structures Guide
Graphs in Data Structures Guide
Graphs
2
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Graph www.foxitsoftware.com/shopping
3
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Graph www.foxitsoftware.com/shopping
D E F G D F G
E
4
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Applications www.foxitsoftware.com/shopping
Maps
5
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Applications www.foxitsoftware.com/shopping
Flight routes
6
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
U X Z
7
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
8
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
9
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Path
A path between two vertices is sequence of alternating vertices and edges, where each
successive vertex is connected.
V
V
U X Z
U X Z
W W
Y Y
10
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Simple Path
A path with distinct nodes
One of the possible paths from u to v is u-w-v
Cycle
A path that starts and finish at same vertex V
V
U X Z
U X Z
W W
Y Y
11
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Reachability of Vertex
A vertex is v reachable from other vertex if there exists a path from other vertex to
vertex v
In undirected graph, edge is considered as 2-ways, so every vertex is reachable in following
example.
V V
U X Z U X Z
U , V and X are not reachable from Z all vertices are reachable from each other
12
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
V V
U X Z U X Z
13
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
V V
U X Z U X Z
14
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
V
U X
U X
X
15
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Sub Graph V
A graph that is consists of subset of vertices and edges of G
U X Z
Two possible sub-graphs
V
U X Z
U X
U X Z
16
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Simple Graph
A graph with no parallel and self edges
V
U X Z
17
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
18
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Path Length
Sum of weights of edges on path from one vertex to other.
Length of path between u and w is 2
Length of path between u and y is 3
V
There are two paths from u to x 1 2
2
Path u-w-x has length 6 and path u-w-y-x has length 5
2
Shortest Path U X Z
Path with minimum length 2 4 1
From u to x is 5
W 2
From u to v is 4
1
Y
19
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
Tree
An undirected graph G is tree if it fulfills any of the following condition:
G has V-1 edges and no cycles
G has V-1 edges and is connected
G is connected, but removing any edge disconnects it
G is acyclic, but adding any edges creates a cycle
Exactly one simple path between each pair of vertices in G
V V
U X Z U X Z
20
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
U X Z
21
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Terminologies www.foxitsoftware.com/shopping
CS17 CS32
CS18 CS123 CS224
22
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Graph ADT www.foxitsoftware.com/shopping
23
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Memory Representation www.foxitsoftware.com/shopping
v5
v2 v4
24
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Adjacency Matrix www.foxitsoftware.com/shopping
V1 V2 V3 V4 V5
V1 0 1 1 0 0
v1 v3 V2 1 0 1 1 0
v5 V3 1 1 0 1 1
V4 0 1 1 0 1
v2 v4 V5 0 0 1 1 0
25
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Adjacency Matrix www.foxitsoftware.com/shopping
If graph is directed?
Then entry Eij is equal to 1 if there exists an edge e = from vi to vj and 0 otherwise.
V1 V2 V3 V4 V5
V1 0 1 1 0 0
v1 v3
V2 0 0 1 0 0
V3 0 0 0 1 1 v5
V4 0 1 0 0 0 v4
v2
V5 0 0 0 1 0
Weighted Graph:
1 and 0 are replaced with respective weights
0 or -1 presents no edge
26
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Example www.foxitsoftware.com/shopping
27
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Adjacency Matrix www.foxitsoftware.com/shopping
28
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Adjacency List www.foxitsoftware.com/shopping
1 “V1” 2 3
2 “V2” 1 3 4
5 v1 v3
3 “V3” 1 2 4
v5
4 “V4” 2 3 5
v2 v4
5 “V5” 3 4
List can be of simple integers/ characters/ strings which represents vertex label or number
Here assumes a hashmap with <key,value> pair where key is vertex label and values is list of its
connected vertices
29
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
www.foxitsoftware.com/shopping
30
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Adjacency List www.foxitsoftware.com/shopping
Running Time?
Get a vertex’s out-edges 1 “V1” 2 3
Get a vertex’s in-edges
2 “V2” 1 3 4
Decide if some edge exists
Insert an edge 3 “V3” 1 2 4 5
Delete an edge
4 “V4” 2 3 5
Inset a vertex
Remove a vertex “V5”
5 3 4
Memory
O(|V|+|E|)
Good for?
Sparse
Edges are significantly less than |V|2
31