Professional Documents
Culture Documents
Graphs
⚫Basic Terminology
⚫Representation of Graphs
⚫Graph Traversals
⚫DFS and BFS
⚫Spanning Trees
⚫Prims Algorithm
⚫Kruskals Algorithm
⚫Dijkstra Algorithm
Introduction to Graphs
Definition: A graph G is a pair, G = (V, E), where V is a finite nonempty
set of vertices and E is called the set of edges.
Example a
:
b
d e
V= {a,b,c,d,e}
E=(a,b), (a,c), (a,d), (b,e), (c,d), (c,e), (d,e)}
Graph Terminology
⮚ If two nodes are connected by an edge, they are neighbors (and the nodes
are adjacent to each other).
⮚ A path is a sequence of nodes such that each node (but the last) is the
predecessor of the next node in the list.
Example: If v1,v2,. . .,vk are vertices then vi and vi+1 should be consecutive.
⮚ The degree of a node is the number of edges it has.
⮚ A cycle in G is a simple path in which the first and last vertices are the
same.
⮚ A graph is termed as weighted graph if all the edges in it are labeled with
some weights.
u v u v
G2 in:1, out:
G1 3 0
1
0
in: 1, out:
3 1 2 3 1 2
3 3 in: 1, out:
2 0
Cyclic Acyclic Weighted Graph
graphA graphA
A
10 9
B C B C
B C
6
D D
Complete Strongly
Graph Connected
A B A
D B
C D
C
E
0 0 0 1 2 0
1 2 1 2 3 1 2
3 3
G1
(i) ii) (iii)
(iv)
0
0 0 0
1 1 1
2 2
G2 (i) (ii) (iii)
Adjacency Matrix
A
A B C D E
A 0 1 1 1 0
B C
B 1 0 1 1 0
C 1 1 0 1 1
D E D 1 1 1 0 1
E 0 0 1 1 0
Linked List Representation
⮚ The header node in each list maintains a list of all adjacent vertices of a
node .
A B C D NULL
A
B A C D NULL
E NULL B C
C A B D
D A B C E NULL D E
N E C D NULL
Undirected Graph Adjacency List Adjacency Matrix
Directed Graph Adjacency List Adjacency Matrix
Graph Traversal Techniques
There are two standard graph traversal techniques:
⮚ Depth-First Search (DFS)
⮚ Breadth-First Search (BFS)
⮚ Traversing a graph means visiting all the vertices in the graph exactly once.
⮚ DFS and BFS traversals result an acyclic graph.
⮚ DFS and BFS traversal on the same graph do not give the same order of visit
of vertices.
Depth First Traversal:
⮚The depth first traversal is similar to the in-order traversal of a binary tree.
⮚An initial or source vertex is identified to start traversing, then from that vertex
⮚All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
B C D
B C D
A
E
B C D
The Depth First Search Tree Order : A, B, E, D, C
A B C A B C
D E F D E F
G H I G H I
DFS Traversal Order BFS Traversal Order
A B C F E G D H I A B D E C G F H
I
Example3: Construct the DFS and BFS for the following graph.
Note: From the above diagram it is possible to say that G and E are never
traversed.
Breadth First Search: The BFS Tree Order : A,B,F,C,D
⮚ Transportation networks
⮚ Highway network
⮚ Flight network
⮚ Computer networks
⮚ Local area network
⮚ Internet
⮚ Web
⮚ Databases
⮚ Entity-relationship diagram
Breadth First Search: The BFS Tree Order : A,B,F,C,D
⮚A spanning tree of a graph is just a sub graph that contains all the vertices and
is a tree. which has all the vertices covered with minimum possible number of
edges. There are no cycles formed in a spanning tree
⮚A graph may have many spanning trees.
o o o
r r r
Minimum Spanning Trees
⮚The minimum spanning tree for a given graph is the spanning tree of
minimum cost for that graph. that has minimum weight than all other
spanning trees of the same graph .
that has minimum
Weighted weight than all other spanning
Graph treesSpanning
Minimum of the same
Tree
graph
7
2 2
5 3 3
4
1 1
Kruskal’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.
C 2 D 3
3 1 2
E F
4
A B
2
C 2 D
2
3 1
E F
Prim’s algorithm
Prim’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.
2. Any vertex v you like is chosen as starting vertex and is marked as visited
(define a cluster C).
3. The smallest- weighted edge e = (v,u), which connects one vertex v inside
the cluster C with another vertex u outside of C, is chosen and is added to
the MST.
C 2 D 3
3 1 2
E F
4
1
5 4
2 5
3 2
3 4
6
6
5 6
The Single-Source Shortest path
Problem ( SSSP)
• Given a positively weighted directed graph G with a
source vertex v, find the shortest paths from v to all
other vertices in the graph.
Ex :-
v V1
V
V5
2 V1V3
V1V3
VV4V V
1 3 4
V2
V V1V5
V3 V6 5) V1V3V4V6 28
4
4
5
5 1
1 0 2 0 5
1 3
1 5
0 5
2 2 3
0 0 0
1
3
3 5
4 6
✔
Iteration S Dist[2 Dist[3] Dist[4] Dist[5] Dist[6]
]
Initial {1} 50 10 ∞ 45 ∞
1 { 1,3 } 50 10 25
✔ 45 ∞
✔
2 { 1,3,4 } 45 10 25 ✔ 45 28
✔
3 { 1,3,4,6 } 45 10 25 45 28
✔ ✔
4 { 1,3,4,5,6 } 45 10 25 45 28
✔ ✔