0% found this document useful (0 votes)
40 views92 pages

Unit 5 - DS - AK2 - Graph

Uploaded by

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

Unit 5 - DS - AK2 - Graph

Uploaded by

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

Data Structures

Dr.S.Nagendra Prabhu
Unit - 5
Graph
Introduction to Graph, Graph Traversal, Topological
sorting, Minimum spanning tree – Prims Algorithm,
Kruskal’s Algorithm, Shortest Path Algorithm -
Dijkstra’s Algorithm
Introduction to Graph
• Definition
• A graph can be defined as group of vertices and edges
that are used to connect these vertices.
• A graph G can be defined as an ordered set G(V, E)
where V(G) represents the set of vertices and E(G)
represents the set of edges which are used to connect
these vertices.
• A Graph G(V, E) with
• 5 vertices (A, B, C, D, E) and
• six edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A))
shown in the following figure.
• Types of Graph

• A graph can be directed or undirected.


• In an undirected graph, edges do not have any direction associated
with them,
• i.e if an edge is drawn between nodes A and B, then the
nodes can be traversed from A to B as well as from B to
A.
• In a directed graph, edges form an ordered pair.
• If there is an edge from A to B, then there is a path from
A to B but not from B to A.
• Examples for directed graph and undirected graph.

A A

B C B C

D E D E

Undirected Graph Directed Graph


Graph structures can be seen every where in the real world
1. Airlines: cities are connected through airlines. Every city is vertex and way
from one city to another city is called an edge.
2. Face book: Each user is represented as a vertex and two people are
friends when there is an edge between two vertices. Similarly friend
suggestion also uses graph theory concept.
3. Google Maps: Various locations are represented as vertices and the roads
are represented as edges and graph theory is used to find shortest path
between two nodes.
4. Etc..,
Terminology of Graph

• Adjacent nodes or neighbors: For every edge, e=(u,v) that


connects nodes u and v, the nodes u and v are the end-points
and are said to be the adjacent nodes or neighbors.
• In the above undirected graph (A,B), (A,C), (B,C), (B,D), (D,C), (D,E),
(C,E) are adjacent nodes.
• Degree of a node: Degree of a node u (deg(u)) is the total
number of edges containing the node u.
• If deg(u)=0, it means that u does not belong to any edge and such a
node is known as an isolated node.
Terminology of a Directed Graph
Out-degree of a node: The out-
degree of a node u, written as
outdeg(u), is the number of edges
that originate at u.
In-degree of a node: The in-degree
of a node u, written as indeg(u), is the indeg(A) = 0
number of edges that terminate at u. outdeg(A) = 2
Degree of a node: The degree of a deg(A) = 0 + 2 = 2
node, written as deg(u), is equal to
the sum of in-degree and out-degree
indeg(D) = 2
of that node.
outdeg(D) = 1
Therefore, deg(u) = indeg(u) +
deg(D) = 2 + 1 = 3
Isolated vertex A vertex with
degree zero. Such a vertex is not
an end-point of any edge.
C is Isolated vertex
Pendant vertex (also known as
leaf vertex) A vertex with degree
one (indegree).
E is Pendant vertex

Source A node u is known as a


source if it has a positive out-
degree but a zero in-degree.
A is Source
C is Sink
Sink A node u is known as a sink
if it has a positive in-degree but a
• Regular graph: it is a graph where each vertex has the
same number of neighbors. That is, every node has
the same degree.
• A regular graph with vertices of degree k is called a k-
regular graph of degree k.
• (0-regular graph) (1-regular graph) (2-regular graph)

A A
A

B C B C
B C

E D E
D D E

F
F F
• Labeled graph or weighted graph: A graph is said to be
labeled if every edge in the graph is assigned some
value (weight).
• In a weighted graph, the edges of the graph are assigned
some weight or length.
• The weight of an edge denoted by w(e) is a positive value
which indicates the cost of traversing the edge.

Example:
• Multiple edges (also called parallel edges or a multi-
edge), are two or more edges that are incident to the
same two vertices.
• A simple graph has no multiple edges.
Strongly connected directed graph: A digraph is said to
be strongly connected if and only if there exists a path
between every pair of nodes in G.
That is, if there is a path from node u to v, then there must
be a path from node v to u.
Graph Representation
• Graph representation, we simply mean the
technique which is to be used in order to store
some graph into the computer's memory.
• There are two ways to store Graph into the
computer's memory
• 1. Sequential Representation
• 2. Linked Representation
Sequential Representation
• In sequential representation, use adjacency matrix to
store the mapping represented by vertices and edges.
• In adjacency matrix, the rows and columns are
represented by the graph vertices. A graph having n
vertices, will have a dimension n x n.
• If there is an edge from row vertex to column vertex it is
given as 1 otherwise 0.
A directed graph and its adjacency matrix
representation is shown in the following figure.
Representation of weighted directed
graph
• It is different. Instead of filling the entry by 1, the Non-
zero entries of the adjacency matrix are represented by
the weight of respective edges.
• Representation of weighted directed graph is different.
Instead of filling the entry by 1, the Non- zero entries of
the adjacency matrix are represented by the weight of
respective edges.
Linked Representation
• In the linked representation, an adjacency list is used to
store the Graph into the computer's memory.
• Consider the undirected graph shown in the following
figure and check the adjacency list representation.
Undirected graph for the adjacency list
representation
• An adjacency list is maintained for each node
present in the graph which stores the node
value and a pointer to the next adjacent node
to the respective node.
• If all the adjacent nodes are traversed then
store the NULL in the pointer field of last node
of the list. The sum of the lengths of adjacency
lists is equal to the twice of the number of
edges present in an undirected graph.
Consider the directed graph and check the
adjacency list representation of the graph.
• In a directed graph, the sum of lengths of all the
adjacency lists is equal to the number of edges present
in the graph.
• In the case of weighted directed graph, each
node contains an extra field that is called the
weight of the node. The adjacency list
representation of a directed graph is shown in
the following figure.
Traversing a Graph
• The graph is one non-linear data structure. That is consists of
some nodes and their connected edges. The edges may be
directed or undirected. This graph can be represented as G(V,
E). The following graph can be represented as G({A, B, C, D,
E}, {(A, B), (B, D), (D, E), (B, C), (C, A)})

• The graph has two types of traversal algorithms.


• These are called the
• Breadth First Search and
• Depth First Search.
Breadth-first search
• Here we use Queue data structure.
• The traversal start from source node, its visit all the
children of the root, then start with first then visits all its
children, this process will repeat until no more element
to visit.
• A standard BFS implementation puts each vertex of the
graph into one of two categories:
1.Visited
2.Not Visited
Breadth-first search (BFS)
• Let's see how the Breadth First Search algorithm works
with an example. We use an undirected graph with 5
vertices.

• We start from vertex 0, the BFS algorithm starts by


putting it in the Visited list and putting all its adjacent
vertices in the Queue.
• Next, we visit the element at the front of queue i.e. 1 and
go to its adjacent nodes. Since 0 has already been visited,
we visit 2 instead.

• Vertex 2 has an unvisited adjacent vertex in 4, so we add


that to the back of the queue and visit 3, which is at the
front of the queue.
• 4 remains in the queue

• Only 4 remains in the queue since the only adjacent node


of 3 i.e. 0 is already visited. We visit it.

• Since the queue is empty, we have completed the


Breadth First Traversal of the graph.
Breadth-first search (BFS) - Algorithm
Traverse the below graph using BFS
Depth First Search
• Here we use stack data structure.
• The traversal start from source node and goes till all the
successor of the first neighbor, then continues with the
remaining nodes and so on.
• A standard DFS implementation puts each vertex of the
graph into one of two categories:
1.Visited
2.Not Visited
Depth First Search Example (DFS)
• Let's see how the Depth First Search algorithm works
with an example. We use an undirected graph with 5
vertices.

• We start from vertex 0, the DFS algorithm starts by


putting it in the Visited list and putting all its adjacent
vertices in the stack.
• Next, we visit the element at the top of stack i.e. 1 and go
to its adjacent nodes. Since 0 has already been visited,
we visit 2 instead.

• Vertex 2 has an unvisited adjacent vertex in 4, so we add


that to the top of the stack and visit it.
• Vertex 2 has an unvisited adjacent vertex in 4, so we add
that to the top of the stack and visit it.

• After we visit the last element 3, it doesn't have any


unvisited adjacent nodes, so we have completed the
Depth First Traversal of the graph.
Depth-first search (DFS) - Algorithm
Traverse the below graph using DFS
Solve BFS and DFS Or the below
graph
Solve BFS and DFS Or the below
graph
Minimum Spanning Tree
Minimum Spanning Tree
• A spanning tree is a subset of Graph G, which has all the
vertices covered with minimum possible number of edges.
• They will have G = (V,E).
• V  Vertices or nodes and E Edges.
Mathematical properties of spanning tree

• Spanning tree has n-1 edges, where n is number of nodes (vertices)


• So we can conclude here that spanning trees are subset of a connected Graph G.
Application of Spanning Tree
• Spanning tree is basically used to find minimum paths to connect all
nodes in a graph.
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
Minimum Spanning Tree
(MST)
• In a weighted graph, a minimum spanning tree is a spanning
tree that has minimum weight that all other spanning trees
of the same graph.
MST Algorithm
• Kruskal’s Algorithm
• Prim’s Algorithm
Both are greedy algorithms.
Greedy Method
• Greedy algorithm obtains an optimal solution by
making a sequence of decisions.
• Decisions are made one by one in some order.
• Each decision is made using a greedy-choice
property or greedy criterion.
• A decision, once made, is (usually) not changed
later.
Example
A cable company want to connect five villages to their
network which currently extends to the market town
of Avonford. What is the minimum length of cable
needed?

Brinleigh 5
Cornwell

3
4
8 6

8
Avonford Fingley Donster
7

5
4
2

Edan
We model the situation as a network, then the problem
is to find the minimum connector for the network

B 5
C

3
4
8 6

8
A F D
7

5
4
2

E
Kruskal’s Algorithm

List the edges in


order of size:
B 5
C
ED 2
3 AB 3
4 AE 4
8 6
CD 4
BC 5
8
EF 5
A D
7 F CF 6
AF 7
5 BF 8
4 CF 8
2

E
Kruskal’s Algorithm

Select the shortest


edge in the network
B 5
C
ED 2
3
4
8 6

8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3 ED 2
4 AB 3
8 6

8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3
ED 2
4 AB 3
8 6
CD 4 (or AE 4)
8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
A D
7 F

5
4
2

E
Kruskal’s Algorithm

Select the next shortest


edge which does not
B 5 create a cycle
C

3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
BC 5 – forms a cycle
A D
7 F EF 5

5
4
2

E
Kruskal’s Algorithm

All vertices have been


connected.
B 5
C
The solution is
3
4 ED 2
8 6
AB 3
CD 4
8
AE 4
A D
7 F EF 5

5
4 Total weight of tree: 18
2

E
Kruskal’s Algorithm

All vertices have been


connected.
B
C
The solution is
3
4 ED 2
AB 3
CD 4
AE 4
A D
F EF 5

5
4 Total weight of tree: 18
2

E
Kruskal Algorithm
Algorithm

• Remove all loops & Parallel Edges from the given graph

• Sort all the edges in non-decreasing order of their weight.

• Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else,
discard it.

• Repeat step#2 until there are (V-1) edges in the spanning tree.
Kruskal Algorithm
Kruskal
Algorithm
Find a MST using Kruskal Algorithm
Kruskal algorithm to create MST
Algorithm
MST-KRUSKAL(G, w)
A←Ø
for each vertex v V[G]
do MAKE-SET(v)
sort the edges of E into nondecreasing order by weight w
for each edge (u, v) E, taken in nondecreasing order by weight
do if FIND-SET(u) ≠ FIND-SET(v)
then A ← A {(u, v)}
UNION(u, v)
return A
Algorithm Analysis
• Time complexity
• O(nlogn) where n is the number of unique characters.
Prim's Spanning Tree Algorithm
• Prim's algorithm to find minimum cost spanning
tree (as Kruskal's algorithm) uses the greedy
approach. Prim's algorithm shares a similarity with
the shortest path first algorithms.

• The steps for implementing Prim's algorithm


are as follows:
1.Initialize the minimum spanning tree with a
vertex chosen at random.
2.Find all the edges that connect the tree to
new vertices, find the minimum and add it to
the tree
3.Keep repeating step 2 until we get a
minimum spanning tree
Prim's Spanning Tree Algorithm
• Example
Prim's Spanning Tree Algorithm
• Step 1 - Remove all loops and parallel edges

• 

• Remove all loops and parallel edges from the given


graph. In case of parallel edges, keep the one
which has the least cost associated and remove all
others.
Prim's Spanning Tree Algorithm
• 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. This node is arbitrarily chosen, so any node
can be the root node. One may wonder why any video can
be a root node. So the answer is, in the spanning tree all the
nodes of a graph are included and because it is connected
then there must be at least one edge, which will join it to the
rest of the tree.
• Step 3 - Check outgoing edges and select the one with
less cost
• After choosing the root node S, we see that S,A and S,C are
two edges with weight 7 and 8, respectively. We choose the
edge S,A as it is lesser than the other.
Prim's Spanning Tree Algorithm
• Now, the tree S-7-A is treated as one node and we
check for all edges going out from it. We select the
one which has the lowest cost and include it in the
tree.

• After this step, S-7-A-3-C tree is formed. Now we'll


again treat it as a node and will check all the edges
again. However, we will choose only the least cost
edge. In this case, C-3-D is the new edge, which is
less than other edges' cost 8, 6, 4, etc.
Prim's Spanning Tree Algorithm
• After adding node D to the spanning tree, we now
have two edges going out of it having the same
cost, i.e. D-2-T and D-2-B. Thus, we can add either
one. But the next step will again yield edge 2 as the
least cost. Hence, we are showing a spanning tree
with both edges included.
Solve using Prim's Spanning
Tree Algorithm
Dijkstra's algorithm
An Introduction to Dijkstra's Algorithm
• Dijkstra's Algorithm is a Graph algorithm that finds the
shortest path from a source vertex to all other vertices in
the Graph (single source shortest path).
• It is a type of Greedy Algorithm that only works on Weighted
Graphs having positive weights.
• The time complexity of Dijkstra's Algorithm is O(V2) with the
help of the adjacency matrix representation of the graph.
• This time complexity can be reduced to O((V + E) log
V) with the help of an adjacency list representation of the
graph, where V is the number of vertices and E is the
number of edges in the graph.
• Dijkstra's Algorithm was designed and published by Dr.
Edsger W. Dijkstra, a Dutch Computer Scientist, Software
Engineer, Programmer, Science Essayist, and Systems
Scientist.
Fundamentals of Dijkstra's Algorithm
• The following are the basic concepts of Dijkstra's Algorithm:
1. Dijkstra's Algorithm begins at the node we select (the
source node), and it examines the graph to find the shortest
path between that node and all the other nodes in the
graph.
2. The Algorithm keeps records of the presently acknowledged
shortest distance from each node to the source node, and it
updates these values if it finds any shorter path.
3. Once the Algorithm has retrieved the shortest path
between the source and another node, that node is marked
as 'visited' and included in the path.
4. The procedure continues until all the nodes in the graph
have been included in the path. In this manner, we have a
path connecting the source node to all other nodes,
following the shortest possible path to reach each node.
Understanding the Working of Dijkstra's
Algorithm
• A graph and source vertex are requirements for Dijkstra's Algorithm. This
Algorithm is established on Greedy Approach and thus finds the locally
optimal choice (local minima in this case) at each step of the Algorithm.
• Each Vertex in this Algorithm will have two properties defined for
it:
1.Visited Property
2.Path Property
• Let us understand these properties in brief.
• Visited Property:
1. The 'visited' property signifies whether or not the node has been visited.
2. We are using this property so that we do not revisit any node.
3. A node is marked visited only when the shortest path has been found.
• Path Property:
1. The 'path' property stores the value of the current minimum path to the node.
2. The current minimum path implies the shortest way we have reached this node till
now.
3. This property is revised when any neighbor of the node is visited.
4. This property is significant because it will store the final answer for each node.
How to find Shortest Paths from Source to
all Vertices using Dijkstra’s Algorithm
• Consider the below graph:
• Step 1: Start from Node 0 and mark Node as visited as
you can check in below image visited Node is marked
red.
• Step 2: Check for adjacent Nodes, Now we have to
choices (Either choose Node1 with distance 2 or either
choose Node 2 with distance 6 ) and choose Node with
minimum distance. In this step Node 1 is Minimum
distance adjacent Node, so marked it as visited and add
up the distance.
• Distance: Node 0 -> Node 1 = 2
• Step 3: Then Move Forward and check for adjacent
Node which is Node 3, so marked it as visited and add
up the distance, Now the distance will be:
• Distance: Node 0 -> Node 1 -> Node 3 = 2 + 5 = 7
• Step 4: Again we have two choices for adjacent Nodes
(Either we can choose Node 4 with distance 10 or either
we can choose Node 5 with distance 15) so choose
Node with minimum distance. In this step Node 4 is
Minimum distance adjacent Node, so marked it as
visited and add up the distance.
• Distance: Node 0 -> Node 1 -> Node 3 -> Node 4
= 2 + 5 + 10 = 17
• Step 5: Again, Move Forward and check for adjacent
Node which is Node 6, so marked it as visited and add
up the distance, Now the distance will be:
• Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 -
> Node 6 = 2 + 5 + 10 + 2 = 19

• So, the Shortest Distance from the Source Vertex


is 19 which is optimal one
Shortest Path Problem
• Single-Source Shortest Path Problem - The problem of finding
shortest paths from a source vertex v to all other vertices in the graph.
Answer
Applications
• - Maps (Map Quest, Google Maps)
• - Routing Systems
Dijkstra's algorithm

• Dijkstra's algorithm - is a solution to the single-source


shortest path problem in graph theory.

• Works on both directed and undirected graphs. However,
all edges must have nonnegative weights.

• Input: Weighted graph G={E,V} and source vertex v∈V,


such that all edge weights are nonnegative

• Output: Lengths of shortest paths (or the shortest paths
themselves) from a given source vertex v∈V to all other
vertices
Approach

1. The algorithm computes for each vertex u the distance to


u from the start vertex v, that is, the weight of a shortest
path between v and u.
2. The algorithm keeps track of the set of vertices for which
the distance has been computed, called the cloud C
3. Every vertex has a label D associated with it. For any
vertex u, D[u] stores an approximation of the distance
between v and u. The algorithm will update a D[u] value
when it finds a shorter path from v to u.
4. When a vertex u is added to the cloud, its label D[u] is
equal to the actual (final) distance between the starting
vertex v and vertex u.
Dijkstra pseudocode
Dijkstra(v1, v2):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.

while List is not empty:


v := remove List vertex with minimum distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v, n)'s weight.

if dist is smaller than n's distance:


n's distance := dist.
n's previous := v.

reconstruct path from v2 back to v1,


following previous pointers.
Example: Initialization
Let us consider A as the source node. Distance of all vertices except source is ∞
Distance(source) = 0

0 ∞
2
A B

4 1 3 10

2 2 ∞
∞ C D E

5 8 ∞ 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance.


Update neighbour’s distance
Select the source node A and check the neighbour nodes it reaches. Update the
weights of B and D as 2 and 1.

0 2
2
A B
1
4 3 10

2 2 ∞
∞ C D E

5 8 1 4 6

Distance(B) = 2 1
F G
Distance(D) = 1
∞ ∞
Now, select the node with minimum distance. Weights are 2 and 1 hence 1 is
selected i.e., D is chosen

0 2
2
A B

4 1 3 10

2 2
∞ C D E ∞

5 8 1 4 6

1
F G

∞ ∞
Neighbors of D are selected and their distances are updated.
Distance of C = Distance of D + weight of D to C = 1+ 2=3
Distance of E = Distance of D + weight of D to E = 1+ 2=3
Distance of F = Distance of D + weight of D to F = 1+ 8=9
Distance of G = Distance of D + weight of D to G = 1+ 4=5
0 2
2
A B

4 1 3 10

2
2
3 C D E 3

5 8 1 4 6

1
F G

9 5
Pick vertex in List with minimum distance and update neighbors.
B has the minimum distance. Neighbours of B are D and E.
Distance of D is not updated since D is already visited.
Previous distance of E = 3
New distance of E = 2+10 = 12
Distance of E not updated since it is larger than previously computed.
0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

9 5
Pick vertex List with minimum distance and update neighbors. E is selected.
Distance of G not updated since it is larger than previously computed.

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

9 5
Pick vertex List with minimum distance and update neighbors.
C is selected. Neighbour is F.
Distance of F = Distance of C + weight of C to F = 3 + 5 = 8

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

8 5
Pick vertex List with minimum distance and update neighbors
G is selected. F is the neighbour.
Previous distance of F is 8.
Distance of F = Distance of G+weight from G to F = 5+1
Update the distance of F as 6.
0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

6 5
F is selected and there are no neighbors to F. This is resultant the shortest path graph

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

6 5

You might also like