Lol Trick
Lol Trick
Graph
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
Graph
• A graph data structure consists of a finite set of ordered pairs,
called edges or arcs, of certain entities called nodes or vertices.
• Every node is linked to its own list that contains the names
of all other nodes that are adjacent to it.
1. Breadth-First Search
2. Depth-First Search
Front=2,Rear=4
Front=3,Rear=5
Front=4,Rear=6
BFS
Front=5,Rear=6
Front=6,Rear=7
Front=7,Rear=9
DFS
Depth First Search (DFS) algorithm traverses a graph in a
depthward motion and uses a stack to remember to get the next
vertex to start a search, when a dead end occurs in any iteration.
• Print all the nodes that can be reached from node H.
DFS
• DFS method uses stack, so push H
onto the stack.
2/
C E
C E
A B D A B D A B D
1/ 1/4 5/ 1/4
C E C E C E
DFS (G) : Example
A B D A B D A B D
5/ 1/4 5/ 1/4 5/8 1/4
A B D A B D
• GT = transpose of directed G.
• GT = (V, ET), ET = {(u, v) : (v, u) E}.
• GT is G with all edges reversed.
a b c d
e f g h
SCC: Example
(1) Run DFS(G) to compute finishing times for all uV
1
a b c d
1
e f g h
SCC: Example
(1) Run DFS(G) to compute finishing times for all uV
1
a b c d
1 10 8 9
3 4 2 7 5 6
e f g h
SCC: Example
(1) Run DFS(G) to compute finishing times for all uV
2
a b c d
11 1 10 8 9
3 4 2 7 5 6
e f g h
SCC: Example
2 1
a b c d
13 14 11 16 1 10 8 9
12 15 3 4 2 7 5 6
e f g h
Vertices sorted according to the finishing times:
b, e, a, c, d, g, h, f
SCC: Example
(2) Compute GT
a b c d
e f g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a b c d
e f g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b c d
e f g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b c d
e f g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b r2= c d
e f g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b r2= c d
e f g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b r2= c d
e f r3= g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b r2= c d
e f r3= g h
SCC: Example
(3) Call DFS(GT) processing vertices in main loop in
decreasing f[u] order: b, e, a, c, d, g, h, f
a r1= b r2= c d
e f r3= g r4=h
SCC: Example
(4) Output vertices of each DFT in DFF as a separate SCC
a r1= b r2= c d
Cc{c,d}
e f r3= g r4=h
Cb{b,a,e}
Cg{g,f} Ch{h}
SCC: Example
a b c d
e f g h
Acyclic component
graph c,d Cc
a,b,e
Cb f,g h
Cg Ch
SCC
Articulation points, bridge and bi-connected
component
• Let G = (V, E) be a connected, undirected graph.
• An articulation point of G is a vertex whose removal
disconnects G.
• A bridge of G is an edge whose removal disconnects G.
• A biconnected component of G is a maximal set of edges such
that any two edges in the set lie on a common simple cycle. We
can determine articulation points, bridges, and biconnected
components using depth-first search.
Shortest Path Algorithms
Need to study three different algorithms to calculate the
shortest path between the vertices of a graph.
2. Dijkstra’s algorithm
3. Warshall’s algorithm
Spanning Tree
• Given a connected, undirected graph, a spanning tree of that graph
is a subgraph that is a tree and connects all the vertices together.
• Removing one edge from the spanning tree will make the graph
disconnected, i.e. the spanning tree is minimally connected.
• Adding one edge to the spanning tree will create a circuit or loop,
i.e. the spanning tree is maximally acyclic.
Mathematical Properties of Spanning Tree
• Spanning tree has n-1 edges, where n is the number of
nodes (vertices).
• From a complete graph, by removing maximum e - n +
1 edges, we can construct a spanning tree.
• A complete graph can have maximum nn-2 number of
spanning trees.
• Thus, we can conclude spanning trees are a subset of
connected Graph G.
• Disconnected graphs do not have spanning tree.
Applications of MST
• Civil Network Planning
Final outcome
Kruskal’s Algorithm
The algorithm aims to find a subset of the edges that
forms a tree that includes every vertex.
Example# Kruskal’s
Example# Kruskal’s
Example# Kruskal’s
Example# Kruskal’s
Example# Kruskal’s
Find MST
Single-Source Shortest Paths
• Find shortest path from G to F
Consider G as
start vertex
Dijkstra's algorithm
Animation for Dijkstra’s Algorithm
• https://www.cs.usfca.edu/~galles/visualization/Dijkstra.html
Dijkstra vs. MST
• MST is used to traverse a graph in efficient manner
while Dijkstra’s algo calculates the distance from a
given vertex to every vertex in graph.
• Then aK [i, j] (the [i,j] entry in the matrix AK) gives the no. of paths of
length K from vi to vj.
Path Matrix
0 1 1 0
0 0 1 1
A1 =
0 0 0 1
1 1 0 0
0 1 1 0 0 1 1 0 0 0 1 2
0 0 1 1 0 0 1 1 1 1 0 1
A2 = A1 * A1 = * =
0 0 0 1 0 0 0 1 1 1 0 0
1 1 0 0 1 1 0 0 0 1 2 1
Path Matrix
0 0 1 2 0 1 1 0 2 2 0 1
1 1 0 1 0 0 1 1 1 2 2 1
A3 = A2 * A1 = * =
1 1 0 0 0 0 0 1 0 1 2 1
0 1 2 1 1 1 0 0 1 1 1 3
2 2 0 1 0 1 1 0 1 3 4 2
1 2 2 1 0 0 1 1 1 2 3 4
A4 = A3 * A1 = * =
0 1 2 1 0 0 0 1 1 1 1 3
1 1 1 3 1 1 0 0 3 4 2 2
Path Matrix
Suppose now we define the matrix Br as follows:
Br =A + A2 + A3 +……..+ Ar
Then the [i,j] entry of the matrix Br gives the no. of paths of length
r or less from vi to vj.
0 1 1 0 0 0 1 2 2 2 0 1 1 3 4 2 3 6 6 5
0 0 1 1 1 1 0 1 1 2 2 1 1 2 3 4 3 5 6 7
= + + + =
0 0 0 1 1 1 0 0 0 1 2 1 1 1 1 3 2 3 3 5
1 1 0 0 0 1 2 1 1 1 1 3 3 4 2 2 5 7 5 6
Path Matrix
Now the path matrix P can be given as
1 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑖 𝑡𝑜 𝑣𝑗
𝑃𝑖𝑗 = ቊ
0 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒