0% found this document useful (0 votes)
136 views32 pages

Introduction to Graph Algorithms

The document discusses graphs and graph algorithms. It defines key graph terminology like vertices, edges, directed/undirected graphs. It also explains different graph representations and traversal algorithms like depth-first search and breadth-first search. Topological sorting of directed acyclic graphs is also covered.

Uploaded by

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

Introduction to Graph Algorithms

The document discusses graphs and graph algorithms. It defines key graph terminology like vertices, edges, directed/undirected graphs. It also explains different graph representations and traversal algorithms like depth-first search and breadth-first search. Topological sorting of directed acyclic graphs is also covered.

Uploaded by

rno68792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT III GRAPHS

Elementary Graph Algorithms

Introduction to Graphs
Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices)
and a set of links known as edges (or Arcs). Here edges are used to connect the vertices. A
graph is defined as follows...
Graph is a collection of vertices and arcs in which vertices are connected with arcs

Graph is a collection of nodes and edges in which nodes are connected with edges
Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of
edges.
Example
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Graph Terminology
We use the following terms in graph data structure...

Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In
above example graph, A, B, C, D & E are known as vertices.

Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph the link between
vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e.,
(A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.

Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge


between vertices A and B then edge (A , B) is equal to edge (B , A).
Directed Edge - A directed egde is a unidirectional edge. If there is directed edge
between vertices A and B then edge (A , B) is not equal to edge (B , A).

Weighted Edge - A weighted egde is a edge with value (cost) on it.

Undirected Graph
A graph with only undirected edges is said to be undirected graph.

Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.

End vertices or Endpoints


The two vertices joined by edge are called end vertices (or endpoints) of that edge.

Origin
If a edge is directed, its first endpoint is said to be the origin of it.

Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is
said to be the destination of that edge.

Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In
other words, vertices A and B are said to be adjacent if there is an edge between them.

Incident
Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.

Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.

Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.

Degree
Total number of edges connected to a vertex is said to be degree of that vertex.

Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.

Parallel edges or Multiple edges


If there are two undirected edges with same end vertices and two directed edges with same
origin and destination, such edges are called parallel edges or multiple edges.

Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.

Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other
vertex such that each edge is incident to its predecessor and successor vertex.
Elementary Graph Algorithms

Graph Representations
Graph data structure is represented using following representations...

1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List

1. Adjacency Matrix
In this representation, the graph is represented using a matrix of size total number of vertices
by a total number of vertices. That means a graph with 4 vertices is represented using a matrix
of size 4X4. In this matrix, both rows and columns represent vertices. This matrix is filled
with either 1 or 0. Here, 1 represents that there is an edge from row vertex to column vertex
and 0 represents that there is no edge from row vertex to column vertex.

For example, consider the following undirected graph representation...

Directed graph representation...


2. Incidence Matrix
In this representation, the graph is represented using a matrix of size total number of vertices
by a total number of edges. That means graph with 4 vertices and 6 edges is represented using
a matrix of size 4X6. In this matrix, rows represent vertices and columns represents edges.
This matrix is filled with 0 or 1 or -1. Here, 0 represents that the row edge is not connected to
column vertex, 1 represents that the row edge is connected as the outgoing edge to column
vertex and -1 represents that the row edge is connected as the incoming edge to column
vertex.

For example, consider the following directed graph representation..

3. Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.

For example, consider the following directed graph representation implemented using linked
list...

This representation can also be implemented using an array as follows..


Graph Traversal
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is
also used to decide the order of vertices is visited in the search process. A graph traversal
finds the edges to be used in the search process without creating loops. That means using
graph traversal we visit all the vertices of the graph without getting into looping path.

There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)

DFS (Depth First Search)

DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Stack data structure with maximum size of total number of vertices
in the graph to implement DFS traversal.

We use the following steps to implement DFS traversal...

 Step 1 - Define a Stack of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it
on to the Stack.
 Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the
top of stack and push it on to the stack.
 Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which
is at the top of the stack.
 Step 5 - When there is no new vertex to visit then use back tracking and pop one
vertex from the stack.
 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Back tracking is coming back to the vertex from which we reached the current vertex.
1.
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Queue data structure with maximum size of total number of vertices
in the graph to implement BFS traversal.

We use the following steps to implement BFS traversal...

 Step 1 - Define a Queue of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert
it into the Queue.
 Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of
the Queue and insert them into the Queue.
 Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
 Step 5 - Repeat steps 3 and 4 until queue becomes empty.
 Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph
Topological Sort

Topological Sort is a linear ordering of the vertices in such a way that


if there is an edge in the DAG going from vertex ‘u’ to vertex ‘v’,
then ‘u’ comes before ‘v’ in the ordering.

It is important to note that-


 Topological Sorting is possible if and only if the graph is a Directed Acyclic Graph.
 There may exist multiple different topological orderings for a given directed acyclic graph.

1. Topological Sort Example

Consider the following directed acyclic graph-

For this graph, following 4 different topological orderings are possible-


 123456
 123465
 132456
 132465
Applications of Topological Sort-
Few important applications of topological sort are-
 Scheduling jobs from the given dependencies among jobs
 Instruction Scheduling
 Determining the order of compilation tasks to perform in makefiles
 Data Serialization
 PRACTICE PROBLEMS BASED ON TOPOLOGICAL SORT-
Problem-01:

Find the number of different topological orderings possible for the given graph-

1. Solution-

The topological orderings of the above graph are found in the following steps-
Step-01: Write in-degree of each vertex-

Step-02:
 Vertex-A has the least in-degree.
 So, remove vertex-A and its associated edges.
 Now, update the in-degree of other vertices.
Step-03:
 Vertex-B has the least in-degree.
 So, remove vertex-B and its associated edges.
 Now, update the in-degree of other vertices.

Step-04:
There are two vertices with the least in-degree. So, following 2 cases are possible-
In case-01,
 Remove vertex-C and its associated edges.
 Then, update the in-degree of other vertices.
In case-02,
 Remove vertex-D and its associated edges.
 Then, update the in-degree of other vertices.
Step-05:
Now, the above two cases are continued separately in the similar manner.
In case-01,
 Remove vertex-D since it has the least in-degree.
 Then, remove the remaining vertex-E.
In case-02,
 Remove vertex-C since it has the least in-degree.
 Then, remove the remaining vertex-E.

Conclusion
For the given graph, following 2 different topological orderings are possible-
 ABCDE
 ABDCE

Strongly Connected Components


A strongly connected component is the portion of a directed graph in which there is a path
from each vertex to another vertex. It is applicable only on a directed graph.
For example:

Let us take the graph below.


The strongly connected components of the above graph are:

Strongly connected components


You can observe that in the first strongly connected component, every vertex can reach the
other vertex through the directed path.

These components can be found using Kosaraju's Algorithm.


Kosaraju's Algorithm

Kosaraju's Algorithm is based on the depth-first search algorithm implemented twice.


Three steps are involved.

Perform a depth first search on the whole graph.

Let us start from vertex-0, visit all of its child vertices, and mark the visited vertices as done.
If a vertex leads to an already visited vertex, then push this vertex to the stack.
For example: Starting from vertex-0, go to vertex-1, vertex-2, and then to vertex-3. Vertex-3
leads to already visited vertex-0, so push the source vertex (ie. vertex-3) into the stack.

DFS on the graph

Go to the previous vertex (vertex-2) and visit its child vertices i.e. vertex-4, vertex-5, vertex-6
and vertex-7 sequentially. Since there is nowhere to go from vertex-7, push it into the stack

DFS on the graph

Go to the previous vertex (vertex-6) and visit its child vertices. But, all of its child vertices are
visited, so push it into the stack
. Stacking
Similarly, a final stack is created.

Final Stack
Reverse the original graph

Perform depth-first search on the reversed graph.

Start from the top vertex of the stack. Traverse through all of its child vertices. Once the
already visited vertex is reached, one strongly connected component is formed.

For example: Pop vertex-0 from the stack. Starting from vertex-0, traverse through its child
vertices (vertex-0, vertex-1, vertex-2, vertex-3 in sequence) and mark them as visited. The
child of vertex-3 is already visited, so these visited vertices form one strongly connected
component
Start from the top and traverse through all the vertices
Go to the stack and pop the top vertex if already visited. Otherwise, choose the top vertex
from the stack and traverse through its child vertices as presented above.
Minimum Spanning Tree

Before knowing about the minimum spanning tree, we should know about the spanning tree.

To understand the concept of spanning tree, consider the below graph:

The above graph can be represented as G(V, E), where 'V' is the number of vertices, and 'E' is
the number of edges. The spanning tree of the above graph would be represented as G`(V`,
E`). In this case, V` = V means that the number of vertices in the spanning tree would be the
same as the number of vertices in the graph, but the number of edges would be different. The
number of edges in the spanning tree is the subset of the number of edges in the original
graph. Therefore, the number of edges can be written as:

E` € E

It can also be written as:

E` = |V| - 1

Two conditions exist in the spanning tree, which is as follows:

o The number of vertices in the spanning tree would be the same as the number of
vertices in the original graph.
V` = V
o The number of edges in the spanning tree would be equal to the number of edges
minus 1.
E` = |V| - 1
o The spanning tree should not contain any cycle.
o The spanning tree should not be disconnected.
o A graph can have more than one spanning tree.
Consider the below graph:

The above graph contains 5 vertices. As we know, the vertices in the spanning tree would be
the same as the graph; therefore, V` is equal 5. The number of edges in the spanning tree
would be equal to (5 - 1), i.e., 4. The following are the possible spanning trees:

What is a minimum spanning tree?

The minimum spanning tree is a spanning tree whose sum of the edges is minimum. Consider
the below graph that contains the edge weight:
The following are the spanning trees that we can make from the above graph.

o The first spanning tree is a tree in which we have removed the edge between the
vertices 1 and 5 shown as below:
The sum of the edges of the above tree is (1 + 4 + 5 + 2): 12
o The second spanning tree is a tree in which we have removed the edge between the
vertices 1 and 2 shown as below:
The sum of the edges of the above tree is (3 + 2 + 5 + 4) : 14
o The third spanning tree is a tree in which we have removed the edge between the
vertices 2 and 3 shown as below:
The sum of the edges of the above tree is (1 + 3 + 2 + 5) : 11
o The fourth spanning tree is a tree in which we have removed the edge between the
vertices 3 and 4 shown as below:
The sum of the edges of the above tree is (1 + 3 + 2 + 4) : 10. The edge cost 10 is
minimum so it is a minimum spanning tree.

General properties of minimum spanning tree:

o If we remove any edge from the spanning tree, then it becomes disconnected.
Therefore, we cannot remove any edge from the spanning tree.
o If we add an edge to the spanning tree then it creates a loop. Therefore, we cannot add
any edge to the spanning tree.
o In a graph, each edge has a distinct weight, then there exists only a single and unique
minimum spanning tree. If the edge weight is not distinct, then there can be more than
one minimum spanning tree.
o A complete undirected graph can have an nn-2 number of spanning trees.
o Every connected and undirected graph contains atleast one spanning tree.
o The disconnected graph does not have any spanning tree.
o In a complete graph, we can remove maximum (e-n+1) edges to construct a spanning
tree.
Let's understand the last property through an example.

Consider the complete graph which is given below:

The number of spanning trees that can be made from the above complete graph equals to n n-
2
= 44-2 = 16.

Therefore, 16 spanning trees can be created from the above graph.

The maximum number of edges that can be removed to construct a spanning tree equals to e-
n+1 = 6 - 4 + 1 = 3.

Application of Minimum Spanning Tree

1. Consider n stations are to be linked using a communication network & laying of


communication links between any two stations involves a cost.
The ideal solution would be to extract a subgraph termed as minimum cost spanning
tree.
2. Suppose you want to construct highways or railroads spanning several cities then we
can use the concept of minimum spanning trees.
3. Designing Local Area Networks.
4. Laying pipelines connecting offshore drilling sites, refineries and consumer markets.
5. Suppose you want to apply a set of houses with
o Electric Power
o Water
o Telephone lines
o Sewage lines
To reduce cost, you can connect houses with minimum cost spanning trees.

Prim’s and Kruskal’s Algorithms-

We have discussed-
 Prim’s and Kruskal’s Algorithm are the famous greedy algorithms.
 They are used for finding the Minimum Spanning Tree (MST) of a given graph.
 To apply these algorithms, the given graph must be weighted, connected and undirected.
Some important concepts based on them are-
Concept-01:
If all the edge weights are distinct, then both the algorithms are guaranteed to find the same
MST.
Example-

Consider the following example-

Here, both the algorithms on the above given graph produces the same MST as shown.
Concept-02:
 If all the edge weights are not distinct, then both the algorithms may not always produce
the same MST.
 However, cost of both the MSTs would always be same in both the cases.
Example-
Consider the following example-
Here, both the algorithms on the above given graph produces different MSTs as shown but the
cost is same in both the cases.
Concept-03:
Kruskal’s Algorithm is preferred when-
 The graph is sparse.
 There are less number of edges in the graph like E = O(V)
 The edges are already sorted or can be sorted in linear time.

Prim’s Algorithm is preferred when-


 The graph is dense.
 There are large number of edges in the graph like E = O(V2).
Concept-04:
Difference between Prim’s Algorithm and Kruskal’s Algorithm-

Prim’s Algorithm Kruskal’s Algorithm

The tree that we are making or growing The tree that we are making or growing
always remains connected. usually remains disconnected.

Kruskal’s Algorithm grows a solution


Prim’s Algorithm grows a solution from
from the cheapest edge by adding the
a random vertex by adding the next
next cheapest edge to the existing tree /
cheapest vertex to the existing tree.
forest.

Prim’s Algorithm is faster for dense Kruskal’s Algorithm is faster for sparse
graphs. graphs.

Prim’s Algorithm-
 Prim’s Algorithm is a famous greedy algorithm.
 It is used for finding the Minimum Spanning Tree (MST) of a given graph.
 To apply Prim’s algorithm, the given graph must be weighted, connected and undirected.
Prim’s Algorithm Implementation-
The implementation of Prim’s Algorithm is explained in the following steps-
Step-01:
 Randomly choose any vertex.
 The vertex connecting to the edge having least weight is usually selected.
Step-02
 Find all the edges that connect the tree to new vertices.
 Find the least weight edge among those edges and include it in the existing tree.
 If including that edge creates a cycle, then reject that edge and look for the next least weight
edge.
Step-03:
 Keep repeating step-02 until all the vertices are included and Minimum Spanning Tree
(MST) is obtained.
Prim’s Algorithm Time Complexity-
Worst case time complexity of Prim’s Algorithm is-
 O(ElogV) using binary heap
 O(E + VlogV) using Fibonacci heap
Time Complexity Analysis
 If adjacency list is used to represent the graph, then using breadth first search, all the
vertices can be traversed in O(V + E) time.
 We traverse all the vertices of graph using breadth first search and use a min heap for
storing the vertices not yet included in the MST.
 To get the minimum weight edge, we use min heap as a priority queue.
 Min heap operations like extracting minimum element and decreasing key value takes
O(logV) time.

So, overall time complexity


= O(E + V) x O(logV)
= O((E + V)logV)
= O(ElogV)

This time complexity can be improved and reduced to O(E + VlogV) using Fibonacci heap.
PRACTICE PROBLEMS BASED ON PRIM’S ALGORITHM-
Problem-01:
Construct the minimum spanning tree (MST) for the given graph using Prim’s Algorithm-

Solution-

The above discussed steps are followed to find the minimum cost spanning tree using Prim’s
Algorithm-
Since all the vertices have been included in the MST, so we stop.
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units

Kruskal’s Algorithm-
 Kruskal’s Algorithm is a famous greedy algorithm.
 It is used for finding the Minimum Spanning Tree (MST) of a given graph.
 To apply Kruskal’s algorithm, the given graph must be weighted, connected and
undirected.
Kruskal’s Algorithm Implementation-
The implementation of Kruskal’s Algorithm is explained in the following steps-
Step-01:
 Sort all the edges from low weight to high weight.
Step-02:
 Take the edge with the lowest weight and use it to connect the vertices of graph.
 If adding an edge creates a cycle, then reject that edge and go for the next least weight
edge.
Step-03:
 Keep adding edges until all the vertices are connected and a Minimum Spanning Tree
(MST) is obtained.

Thumb Rule to Remember


The above steps may be reduced to the following thumb rule-
 Simply draw all the vertices on the paper.
 Connect these vertices using edges with minimum weights such that no cycle
gets formed.

Kruskal’s Algorithm Time Complexity-

Worst case time complexity of Kruskal’s Algorithm


= O(ElogV) or O(ElogE)

Analysis-
 The edges are maintained as min heap.
 The next edge can be obtained in O(logE) time if graph has E edges.
 Reconstruction of heap takes O(E) time.
 So, Kruskal’s Algorithm takes O(ElogE) time.
 The value of E can be at most O(V2).
 So, O(logV) and O(logE) are same.

Special Case-
 If the edges are already sorted, then there is no need to construct min heap.
 So, deletion from min heap time is saved.
 In this case, time complexity of Kruskal’s Algorithm = O(E + V)
PRACTICE PROBLEMS BASED ON KRUSKAL’S ALGORITHM-
Problem-01:
Construct the minimum spanning tree (MST) for the given graph using Kruskal’s Algorithm-

Solution-
To construct MST using Kruskal’s Algorithm,
 Simply draw all the vertices on the paper.
 Connect these vertices using edges with minimum weights such that no cycle gets formed.

(i) (ii)

(iii) (iv)
(v) (vi)

(vii)
Since all the vertices have been connected / included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units

All -pairs shortest paths:Floyd Warshall Algorithm-

 Floyd Warshall Algorithm is a famous algorithm.


 It is used to solve All Pairs Shortest Path Problem.
 It computes the shortest path between every pair of vertices of the given graph.
 Floyd Warshall Algorithm is an example of dynamic programming approach.
Advantages-
Floyd Warshall Algorithm has the following main advantages-
 It is extremely simple.
 It is easy to implement.
Algorithm- Floyd Warshall Algorithm is as shown below-
Time Complexity-
 Floyd Warshall Algorithm consists of three loops over all the nodes.
 The inner most loop consists of only constant complexity operations.
 Hence, the asymptotic complexity of Floyd Warshall algorithm is O(n 3).
 Here, n is the number of nodes in the given graph.
When Floyd Warshall Algorithm Is Used?
 Floyd Warshall Algorithm is best suited for dense graphs.
 This is because its complexity depends only on the number of vertices in the given graph.
 For sparse graphs, Johnson’s Algorithm is more suitable.
PRACTICE PROBLEM BASED ON FLOYD WARSHALL ALGORITHM
Consider the following directed weighted graph-

Using Floyd Warshall Algorithm, find the shortest path distance between every pair of
vertices.
Solution-
Step-01:
 Remove all the self loops and parallel edges (keeping the lowest weight edge) from the
graph.
 In the given graph, there are neither self edges nor parallel edges.
Step-02:
 Write the initial distance matrix.
 It represents the distance between every pair of vertices in the form of given weights.
 For diagonal elements (representing self-loops), distance value = 0.
 For vertices having a direct edge between them, distance value = weight of that edge.
 For vertices having no direct edge between them, distance value = ∞.
Initial distance matrix for the given graph is-
Step-03:
Using Floyd Warshall Algorithm, write the following 4 matrices-
The last matrix D4 represents the shortest path distance between every pair of vertices.
Remember-
 In the above problem, there are 4 vertices in the given graph.
 So, there will be total 4 matrices of order 4 x 4 in the solution excluding the initial distance
matrix.
 Diagonal elements of each matrix will always be 0.

You might also like