0% found this document useful (0 votes)
22 views109 pages

Lol Trick

A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. It consists of vertices, which represent objects, and edges that connect the vertices and represent links between objects. A graph can be used to represent many real-world networks like transportation networks. Common graph traversal algorithms are breadth-first search (BFS) and depth-first search (DFS), which use queues and stacks respectively to systematically explore the graph. BFS explores neighboring nodes level-by-level while DFS explores deep down one branch before backtracking.

Uploaded by

duttnikhil2403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views109 pages

Lol Trick

A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. It consists of vertices, which represent objects, and edges that connect the vertices and represent links between objects. A graph can be used to represent many real-world networks like transportation networks. Common graph traversal algorithms are breadth-first search (BFS) and depth-first search (DFS), which use queues and stacks respectively to systematically explore the graph. BFS explores neighboring nodes level-by-level while DFS explores deep down one branch before backtracking.

Uploaded by

duttnikhil2403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

Nonlinear Data Structure:

Graph

Dr. Jyoti Srivastava


Graph
• A graph is a pictorial representation of a set of objects where
some pairs of objects are connected by links.

• The interconnected objects are represented by points termed


as vertices, and the links that connect the vertices are
called edges.

• Formally, a graph is a pair of sets (V, E), where V is the set


of vertices and E is the set of edges, connecting the pairs of
vertices.
Graph: Example

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.

• A graph is often viewed as a generalization of the tree structure,


where complex relationship can be represented.

• Example: Graph can be used to represent Transportation network.


nodes - airports
edges - airline flights.
Graphs
• Directed graph (di-graph) if all the edges are directed

• Undirected graph (graph) if all the edges are undirected


Graph Terminology
• Adjacent Nodes: Two vertices
are adjacent if they are endpoints of
the same edge.

• An edge is incident on a vertex if the


vertex is an endpoint of the edge.

• Outgoing edges of a vertex are directed


edges that the vertex is the origin.

• Incoming edges of a vertex are


directed edges that the vertex is the
destination.
Graph Terminology
• Degree of a vertex, v, denoted deg(v)
is the number of incident edges.

• Out-degree, outdeg(v), is the number


of outgoing edges.

• In-degree, indeg(v), is the number of


incoming edges.
Graph Terminology
A path is a sequence of vertices such that
each vertex is adjacent to the next. In a
path, each edge can be traveled only once.

• The length of a path is the number of edges


in that path.

• Cycle(loop) is a path that starts and end at


the same vertex.

• In a weighted graph, every edge is assigned


some weight or length(positive value).
Graph and Adjacency Matrix
Adjacency List
• Another way to represent graphs in memory of computer.

• This consists of a list of nodes.

• Every node is linked to its own list that contains the names
of all other nodes that are adjacent to it.

• It provides ease to follow and clearly show adjacent nodes


of a particular node.
Adjacency List
Adjacency List
Elementary Graph Operations
• How to traverse a Graph?
• There are two standard methods:

1. Breadth-First Search
2. Depth-First Search

BFS uses queue as an temporary data structure for


further processing while DFS uses stack.
BFS
• To traverse a graph, we consider A as root element.

• During the execution QUEUE arrays.

• QUEUE is used to hold the nodes that have to be


processed.
BFS
BFS
Front=1,Rear=1

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.

• Pop and print top element and push


all adjacency neighbors of top
element from lists
DFS: H, I, F, C, G
DFS: H, I, F, C, G, B, E
BFS(G, s)
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE
3 d[u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE
14 then color[v] ← GRAY
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK
BFS(G, s)
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE
3 d[u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE
14 then color[v] ← GRAY
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK
BFS(G, s)
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE
3 d[u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE
14 then color[v] ← GRAY
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK
BFS(G, s)
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE
3 d[u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE
14 then color[v] ← GRAY
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK
BFS(G, s)
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE
3 d[u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE
14 then color[v] ← GRAY
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE
3 π[u] ← NIL
4 time ← 0
5 for each vertex u ∈ V [G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] ← time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ▹ Blacken u; it is finished.
9 f [u] ← time ← time +1 Vertices are timestamped by discovery time/finishing time
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE
3 π[u] ← NIL
4 time ← 0
5 for each vertex u ∈ V [G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] ← time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ▹ Blacken u; it is finished.
9 f [u] ← time ← time +1
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE
3 π[u] ← NIL
4 time ← 0
5 for each vertex u ∈ V [G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] ← time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ▹ Blacken u; it is finished.
9 f [u] ← time ← time +1
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE
3 π[u] ← NIL
4 time ← 0
5 for each vertex u ∈ V [G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] ← time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ▹ Blacken u; it is finished.
9 f [u] ← time ← time +1
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE
3 π[u] ← NIL
4 time ← 0
5 for each vertex u ∈ V [G]
6 do if color[u] = WHITE
The running time of DFS is
7 then DFS-VISIT(u)
Θ(V + E)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] ← time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ▹ Blacken u; it is finished.
9 f [u] ← time ← time +1
DFS (G) : Example A B D
A B D 1/
1/

2/
C E
C E

A B D A B D A B D

1/ 1/4 5/ 1/4

2/3 2/3 2/3

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

6/ 2/3 6/7 2/3 6/7 2/3


C E C E C E

A B D A B D

9/ 5/8 1/4 5/8 1/4


9/10

6/7 2/3 6/7 2/3


C E C E
Directed Acyclic Graph
DAG – Directed graph with no cycles.
Strongly Connected Components
• A classic application of depth-first search: decomposing a directed
graph into its strongly connected components.
• G is strongly connected if every pair (u, v) of vertices in G is
reachable from one another.
• A strongly connected component (SCC) of G is a maximal set of
vertices C  V such that for all u, v  C, both u v and v u
exist.
Strongly Connected Components (SCC)
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 in each tree in the depth-first forest formed in
line 3 as a separate Strongly Connected Component

Time: (V + E).


Transpose of a Directed Graph

• GT = transpose of directed G.
• GT = (V, ET), ET = {(u, v) : (v, u)  E}.
• GT is G with all edges reversed.

• Can create GT in Θ(V + E) time if using adjacency lists.

• G and GT have the same SCC’s. (u and v are reachable from


each other in G if and only if reachable from each other in
GT.)
SCC: Example

a b c d

e f g h
SCC: Example
(1) Run DFS(G) to compute finishing times for all uV
1
a b c d
1

e f g h
SCC: Example
(1) Run DFS(G) to compute finishing times for all uV
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 uV
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.

1. Minimum Spanning Tree

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.

• A spanning tree is a subset of Graph G, which has all the


vertices covered with minimum possible number of edges.
Hence, a spanning tree does not have cycles and it cannot be
disconnected.

• By this definition, we can draw a conclusion that every connected


and undirected Graph G has at least one spanning tree. A
disconnected graph does not have any spanning tree, as it cannot be
spanned to all its vertices.
Spanning Tree
A complete undirected graph can
have maximum nn-2 number of
spanning trees, where n is the
number of nodes.
Minimum Spanning Tree
• We can also assign a weight to each edge, which is a number
representing how unfavourable it is, and use this to assign a
weight to a spanning tree by computing the sum of the weights
of the edges in that spanning tree.

• In real-world situations, this weight can be measured as


distance, congestion, traffic load or any arbitrary value denoted
to the edges.

• Many spanning trees are possible from a single graph.


Minimum Spanning Tree
A minimum spanning tree (MST) or minimum weight
spanning tree is then a spanning tree with weight less than
or equal to the weight(the sum of the weights of its edges)
of every other spanning tree.
Minimum Spanning Tree
General Properties of Spanning Tree
• A connected graph G can have more than one spanning tree.

• All possible spanning trees of graph G, have the same number of


edges and vertices.

• The spanning tree does not have any cycle (loops).

• 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

• Computer Network Routing Protocol: MSTs are applied in


routing algorithm for finding the most efficient path.

• To find airline routes. Vertices denote cities, edges represent


routes between cities.

• Network Designs. Like telephone network, to choose least


costly path.
Example: Consider, city network as a huge graph and now
plans to deploy telephone lines in such a way that in minimum
lines we can connect to all city nodes.
Minimum Spanning-Tree Algorithm
• Prim's Algorithm
• Kruskal's Algorithm

Both are greedy algorithms.


• At each step of an algorithm, one of several possible choices must be made.
• The greedy strategy advocates making the choice that is the best at the
moment.
• Such a strategy is not generally guaranteed to find globally optimal solutions
to problems.
• It makes a locally optimal choice in the hope that this choice will lead to a
globally optimal solution.
Prim’s Algorithm
• Tree Vertices: Vertices that are a part of the MST
• Fringe Vertices: Vertices that are currently not a part of
MST, but adjacent to some tree vertex.
Prim's Spanning Tree Algorithm: Example
Step 1 - Remove all loops and parallel edges
In case of parallel edges, keep the one which has the least cost
associated and remove all others.
Prim's Spanning Tree Algorithm: Example
Step 2 - Choose any arbitrary node as root node
In this case, we choose S node as the root node of Prim's spanning
tree
Step 3 - Check outgoing edges and select the one with
less cost
Prim's Spanning Tree Algorithm: Example
Example# Prim’s
Example# Prim’s
Example# Prim’s
Example# Prim’s

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

• The weight of path p =

• We define the shortest-path weight from u to v by


Single-Source Shortest Paths
• Edge weights can be used to represent
• distances
• Time
• Cost
• Penalties
• loss,
• or any other quantity that accumulates linearly along a path and
that one wishes to minimize.

• The breadth-first-search algorithm is a shortest-paths algorithm


that works on unweighted graphs, that is, graphs in which each
edge can be considered to have unit weight.
Dijkstra's algorithm
Dijkstra's algorithm, is a graph search algorithm that solves the
single source shortest path problem.

• Both directed and undirected graphs


• All edges must have nonnegative weights
• Graph must be connected

It finds shortest path in a Weighted graph [G = (E,V)] from source


vertex s ∈ V to all vertices v ∈ V.

This algorithm is often used in network routing algorithms.


Pseudocode
Example

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.

• Prim’s algo stores a minimum cost edge while


Dijkstra’s algo is used to stores total cost from a
source node to the current node.
Path Matrix
• If A is the adjacency matrix of a graph G.

• 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.

In the following example:


B4 =A + A2 + A3 +A4

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 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Or 𝑃𝑖𝑗 = 1 if and only if there is a nonzero number in the


[i,j] entry of the matrix B.
3 6 6 5 1 1 1 1
3 5 6 7 1 1 1 1
B= P=
2 3 3 5 1 1 1 1
5 7 5 6 1 1 1 1
Warshall’s Algorithm
3 6 6 5
3 5 6 7
B4 =
2 3 3 5
5 7 5 6

You might also like