Graph
Graph
Applications:
Airlines, analysis of electrical circuits, source and destination networks, finding
shortest routes etc.
1 2
3 4
V(G)={1,2,3,4,5}
E(G)={(1,2),(1,3),(2,4),(3,4),(2,5),(4,5)}
1 2
3 4
V(G)={1,2,3,4,5}
E(G)={(1,2),(3,1),(3,4),(2,4),(2,5),(4,5)}
2
6
4
7
Self Loop: If there is an edge whose starting and ending vertices are same that is
(v2,v2) is an edge then it is called a self loop or simply a loop.
Parallel Edges: If there are more than one edge between the same pair of vertices
then they are known as parallel edges.
Degree of a vertex: Degree of a vertex is the number of edges incident to that vertex. In an
undirected graph, the number of edges connected to a vertex/node is called the degree of
that node.
In a digraph/directed graph, there are two types of degrees for every node:
1. In Degree: it is the number of edges coming to that vertex or the number of edges
incident to it.
2. Out Degree: it is the number of edges going outside from that vertex or the number of
edges incident from it.
Simple Graph: A graph or directed graph which does not have self-loop or parallel edges is
called a simple graph.
Multi Graph: A graph or directed graph which has either a self-loop or parallel edges or
both is called a multi graph.
Madhvi Gaur (Assistant Professor)
Complete Graph: A graph G is said to be complete if every vertex in G is
connected to every other vertex in G. Thus a complete graph G must be connected.
The complete graph with n vertices is denoted by Kn.
Cycle: If there is a path containing one or more edges which starts from a vertex
and terminates into the same vertex then the path is called as a cycle.
1
1 2 3 4
1 0 1 1 0
2 2 1 0 1 0
4
3 1 1 0 1
4 1 0 0 0
3
1 2 4 X 1 2 3
2 5 X
3 6 5 X
4 2 X
5 4 X 4 5 6
6 6 X
Note: The following algorithm will process only those nodes which are reachable from the
starting node A.
C D
Step 3: Remove the Front element B from the Queue. Then push all the adjacent vertices of
B to the Queue if it is not in Queue.
0 1 2 3 4
C D E
F R
Visited Node : A, B
A B C D E
Madhvi Gaur (Assistant Professor) ø A A A B
Step 4: Remove the Front element C from the Queue. Then push all the adjacent
vertices of C to the Queue if it is not in Queue.
0 1 2 3 4
D E
F R A B C D E
Visited Node: A, B, C ø A A A B
Step 5: Remove the Front element D from the Queue. Then push all the adjacent
vertices of D to the Queue if it is not in Queue.
0 1 2 3 4
E
F R
Visited Node: A,B,C,D
Q A B C D E
ø A A A B
Madhvi Gaur (Assistant Professor) O
Step 6: Remove the Front element E from the Queue. Then push all the adjacent
vertices of E to the Queue if it is not in Queue. As there is no adjacent vertex of E.
We directly remove the front element E.
0 1 2 3 4
F=-1
R=-1
Visited Node: A,B,C,D,E
So A,B,C,D,E is the BFS traversal of the given Graph.
5 4
19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree
UNION(x, y)
LINK(FIND-SET(x), FIND-SET(y))
LINK(x, y)
1. if key[x] > key[y]
2. then p[y] ← x
3. else p[x] ← y
4. if key[x] = key[y]
5. then key[y] ← key[y] + 1
FIND-SET(x)
1. if x != p[x]
2. then p[x] ← FIND-SET(p[x])
3. return p[x]
Madhvi Gaur (Assistant Professor)
Kruskal Algorithm
b c d 9
11 i 14 e
a
6
7
10
8
h g f
1 2
• Note: we should select safe edge so that it will not create a cycle.
b c d 9
i e
a
6
8
h g f
1 2
• Total Cost = 45
Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST
(known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
DIJKSTRA(G,w,s)
1. INITIALIZE_SINGLE_SOURCE(G,s)
2. S<-ø
3. Q<-V[G]
4. While Q!= ø
5. do u <- EXTRACT_MIN(Q)
6. S<- S U {u}
7. for each vertex v ɛ Adj[u]
8. do RELAX (u,v,w)
Madhvi Gaur (Assistant Professor)
INITIALIZE_SINGLE_SOURCE(G,s)
1. For each vertex v ɛ V[G]
2. do d[v] <- ∞
3. 𝜋[v] <- NIL
4. d[s] <- 0
where d[v] : represents the shortest distance of v from the source and
𝝅[v] : represents the predecessor of node v.
2
B D
10
1 4 8 9
7
A
3 C E
2
2
∞ ∞
10
1 4 8 9
7
0
3 ∞ ∞
2
S={ }
2
∞ ∞
10
1 4 8 9
7
0
3 ∞ ∞
2
S={ A}
10 3
2
10 ∞
10
1 4 8 9
7
0
3 3 ∞
2
S={ A}
10 3
2
10 ∞
10
1 4 8 9
7
0
3 3 ∞
2
S={ A,C}
2
7 11
10
1 4 8 9
7
0
3 3 5
2
S={ A,C}
2
7 11
10
1 4 8 9
7
0
3 3 5
2
S={ A,C,E}
1 4 8 9
7
0
3 3 5
S={ A,C,E} 2
1 4 8 9
7
0
3 3 5
2
S={ A,C,E,B}
10 3
2 7 11 5
7 9 7 11
10 9
1 4 8 9
7
0
3 3 5
2
S={ A,C,E,B}
10
1 4 8 9
7
0
3 3 5
2
S={ A,C,E,B,D}
7 11 5
2 7 11
7 9
9
10
1 4 8 9
7
0
3 3 5
2
S={ A,C,E,B,D}
No changes will be done.
• G is strongly connected, all vertices can be reached from all other vertices.
• H is not, cannot reach any vertex from a.
Madhvi Gaur (Assistant Professor)
In other words, two vertices of directed graph are in the same component if and only
if they are reachable from each other.
STRONGLY-CONNECTED-COMPONENTS (G)
1. Call DFS(G) to compute finishing times f[u] for all u.
2. Compute GT
3. Call DFS(GT), but in the main loop of DFS, consider vertices in order of
decreasing f[u] (as computed in line 1)
4. Output the vertices of each tree in the depth-first forest formed in line 3 as a
separate SCC.
a b c d
e f g h
2. Compute GT
3. Call DFS(GT) but this time consider the vertices in order to decreasing finish
time.
4. Output the vertices of each tree in the DFS-forest as a separate strongly connected
components.
{a, b, e}, {c, d}, {f, g}, and {h}