ADS_Unit_5
ADS_Unit_5
Prepared By,
M.Gouthamm, Asst.Prof, CSE, MRUH
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)}.
2
Introduction to Graphs
Graph Terminology
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)).
3
Graph Representations
Graph data structure is represented using following representations...
1. Edge List
2. Adjacency Matrix
3. Incidence Matrix
4. Adjacency List
1. Edge Lists
An edge list is a list or array of all the edges in a graph. Edge lists are one of the easier
representations of a graph.
In this implementation, the underlying data structure for keeping track of all the nodes and edges is
a single list of pairs. Each pair represents a single edge and is comprised of the two unique IDs of the
nodes involved. Each line/edge in the graph gets an entry in the edge list, and that single data
structure then encodes all nodes and relationships.
4
Graph Representations
2. 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:
5
Graph Representations
3. 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.
6
Graph Representations
4. 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...
7
Directed Acyclic Graph
A Directed Acyclic Graph, often abbreviated as DAG, is a fundamental concept in graph theory. DAGs are
used to show how things are related or depend on each other in a clear and organized way.
A Directed Acyclic Graph (DAG) is a directed graph that does not contain any cycles.
Below Graph represents a Directed Acyclic Graph (DAG):
8
DAG: Properties
Properties of Directed Acyclic Graph DAG:
Directed Acyclic Graph (DAG) has different properties that make them usable in graph problems.
There are following properties of Directed Acyclic Graph (DAG):
1.Reachability Relation: In DAG, we can determine if there is a reachability relation between two
nodes. Node A is said to be reachable from node B if there exists a directed path that starts at node B
and ends at node A. This implies that you can follow the direction of edges in the graph to get from B to
A.
2.Transitive Closure: The transitive closure of a directed graph is a new graph that represents all the
direct and indirect relationships or connections between nodes in the original graph. In other words, it
tells you which nodes can be reached from other nodes by following one or more directed edges.
9
DAG: Properties
3. Transitive Reduction: The transitive reduction of a directed graph is a new graph that retains only
the essential, direct relationships between nodes, while removing any unnecessary indirect edges. In
essence, it simplifies the graph by eliminating edges that can be inferred from the remaining edges.
4. Topological Ordering: A DAG can be topologically sorted, which means you can linearly order its
nodes in such a way that for all the edges, start node of the edge occurs earlier in the sequence. This
property is useful for tasks like scheduling and dependency resolution.
10
DAG: Topological sorting
Topological sorting only exists in Directed Acyclic Graph (DAG). If the nodes of a graph are connected
through directed edges and the graph does not contain a cycle, it is called a directed acyclic
graph(DAG).
The topological sorting of a directed acyclic graph is nothing but the linear ordering of vertices such that
if there is an edge between node u and v(u -> v), node u appears before v in that ordering.
If we try to get topological sorting of this cyclic graph, for edge 1->2, node 1 must appear before 2, for
edge 2->3, node 2 must appear before 3, and for edge 3->1, node 3 must appear before 1 in the linear
ordering.
But such ordering is not possible as there exists a cyclic dependency in the graph. Thereby, topological
sorting is only possible for a directed acyclic graph.
11
DAG: Topological sorting
Approach:
We will be solving it using the DFS traversal technique. DFS goes in-depth, i.e., traverses all nodes by
going ahead, and when there are no further nodes to traverse in the current path, then it backtracks on
the same path and traverses other unvisited nodes.
The algorithm steps are as follows:
1.We must traverse all components of the graph.
2.Make sure to carry a visited array(all elements are initialized to 0) and a stack data structure, where we
are going to store the nodes after completing the DFS call.
3.In the DFS call, first, the current node is marked as visited. Then DFS call is made for all its adjacent
nodes.
4.After visiting all its adjacent nodes, DFS will backtrack to the previous node and meanwhile, the current
node is pushed into the stack.
5.Finally, we will get the stack containing one of the topological sortings of the graph.
12
DAG: Topological sorting
Let’s quickly understand the algorithm considering the following graph:
1.DFS will start from node 0 and mark it as visited. But it has no adjacent nodes. So the DFS will return
putting node 0 into the stack( stack: {0} ).
2.Then DFS will again start from node 1 and mark it as visited, but it also has no adjacent nodes. So node
1 is pushed into the stack( stack: {1, 0} ) and the DFS call will be over.
3.Then DFS will start from node 2 and mark it as visited as well. It will again call DFS for its adjacent node
3 and mark 3 as visited. After visiting node 3, it will find out that only adjacent node 1 is previously
visited.
4.So it will backtrack to node 2, putting node 3 first and then node 2 into the stack ( stack: {2, 3, 1, 0} ).
5.Again, DFS will start from node 4 and mark it as visited. It will find all its adjacent nodes 0 and 1 have
been previously visited. So, node 4 will be pushed into the stack( stack: {4, 2, 3, 1, 0} ).
6.Lastly, DFS will start from node 5 and mark it as visited. Again, it will find all its adjacent nodes 0 and 2
are previously visited. So, it will return putting node 5 into the stack( stack: {5, 4, 2, 3, 1, 0} ).
7.Finally, the stack will contain {5, 4, 2, 3, 1, 0}, which is one of the topological sortings of the graph.
13
DAG: Topological sorting
Let’s understand how the linear orderings are maintained considering the following simple graph:
The linear ordering for the above graph can be 1, 2, 3, 4, 5, 6 or 1, 2, 3, 4, 6, 5. If we closely observe
this algorithm, it is designed in such a way that when the DFS call for a node is completed, the node is
always kept in the stack.
So for example, if the DFS call for 3 is over, we must have the nodes 3, 4, 5, and 6 linearly ordered in
the stack. And this is true for every other node. Thus the linear ordering is always maintained for each
node of the graph.
14
Kahn’s Algorithm: Topological sorting
Kahn’s Algorithm for Topological Sorting is a method used to order the vertices of a directed graph in a
linear order such that for every directed edge from vertex A to vertex B, A comes before B in the order.
The algorithm works by repeatedly finding vertices with no incoming edges, removing them from the
graph, and updating the incoming edges of the remaining vertices. This process continues until all vertices
have been ordered.
Algorithm:
1. Add all nodes with in-degree 0 to a queue.
2. While the queue is not empty:
i. Remove a node from the queue.
ii. For each outgoing edge from the removed node, decrement the in-degree of the destination node by 1.
iii. If the in-degree of a destination node becomes 0, add it to the queue.
3. If the queue is empty and there are still nodes in the graph, the graph contains a cycle and cannot be
topologically sorted.
4. The nodes in the queue represent the topological ordering of the graph.
Example:
Input: V=6 , E = {{2,3}, {3,1}, {4,0}, {4,1}, {5,0}, {5,2}}
Output: 4 5 2 0 3 1
Explanation: In the above output, each dependent vertex is printed after the vertices it
depends upon.
16
DAG: Topological sorting
Ex: Input: V=5 , E={{0,1}, {1,2}, {3,2}, {3,4}}
Output: 0 3 4 1 2
Explanation: In the above output, each dependent vertex is printed after the vertices it
depends upon.
17
DAG: Topological sorting
Ex:
Visualization URL: Topological Sort (DFS) Visualization
18
All Pairs Shortest Path: Floyd-Warshall
In the all pairs shortest path problem, we are to find a shortest path between every pair of vertices in
a directed graph G. That is, for every pair of vertices (i, j), we are to find a shortest path from i to j as
well as one from j to i. These two paths are the same when G is undirected.
When no edge has a negative length, the all-pairs shortest path problem may be solved by using
Dijkstra’s greedy single source algorithm n times, once with each of the n vertices as the source
vertex.
The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the length of a
shortest path from i to j. The matrix A can be obtained by solving n single-source problems using the
algorithm shortest Paths. Since each application of this procedure requires O (n2) time, the matrix A
can be obtained in O (n3) time.
The Floyd Warshall Algorithm is an all-pair shortest path algorithm that uses Dynamic Programming
to find the shortest distances between every pair of vertices in a graph, unlike Dijkstra and Bellman-
Ford which are single source shortest path algorithms. This algorithm works for both the directed and
undirected weighted graphs and can handle graphs with both positive and negative weight edges.
Note: It does not work for the graphs with negative cycles (where the sum of the edges in a cycle is
negative).
19
All Pairs Shortest Path: Floyd-Warshall
All Pairs Shortest Path Algorithm is also known as the Floyd-Warshall algorithm. And this is an
optimization problem that can be solved using dynamic programming.
Let G = <V, E> be a directed graph, where V is a set of vertices and E is a set of edges with
nonnegative length.
L[i, j] = w (i, j), if i ≠ j and (i, j) ∈ E // w(i, j) is the weight of the edge (i, j)
20
All Pairs Shortest Path: Floyd-Warshall
Principle of optimality :
If k is the node on the shortest path from i to j, then the path from i to k and k to j, must also be
shortest.
While considering kth vertex as intermediate vertex, there are two possibilities :
If k is not part of shortest path from i to j, we keep the distance D[i, j] as it is.
If k is part of shortest path from i to j, update distance D[i, j] as: D[i, k] + D[k, j].
21
All Pairs Shortest Path: Floyd-Warshall
Algorithm for All Pairs Shortest Path
This approach is also known as the Floyd-warshall shortest path algorithm. The algorithm for all pair
shortest path (APSP) problem is described below
Algorithm FLOYD_APSP ( L)
// L is the matrix of size n n representing original graph
// D is the distance matrix
D ← L
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
Dk [i, j] ← min { Dk – 1
[i, j], Dk – 1
[i, k] + Dk – 1
[k, j] }
end
end
end
return D
22
All Pairs Shortest Path: Floyd-Warshall
It is an algorithm for finding the shortest path between all the pairs of vertices in a given edge-
weighted directed Graph.
The Floyd-Warshall Algorithm is for solving all pairs of shortest-path problems.
Steps to solve:
1. Construct a Matrix for the given graph G by the following ways,
2. Cost of the graph is cost of each edges and cost(i,i)=0
3. If there is an edge between i and j then cost(i,j)=cost of the edge from i to j
4. If there is no edge then cost(i,j) = ∞
5. Calculate the shortest path between any two vertices using intermediary vertex.
6. The minimum cost can be calculated using the formula,
Ak [i, j] = min{ Ak – 1 [i, j], Ak – 1 [i, k] + Ak – 1 [k, j] }
Problem: Apply Floyd’s Warshall method to find the All pairs shortest path for the
examples
23
All Pairs Shortest Path: Floyd-Warshall
6
Example 1: 1 2
4
3
11 2
3
All Pairs Shortest Path: Floyd-Warshall
All Pairs Shortest Path: Floyd-Warshall
Now we will calculate A3
All Pairs Shortest Path: Floyd-Warshall
Example 2:
8
1 2
7 5 2
2
4 3
1
All Pairs Shortest Path: Floyd-Warshall
Example 3:
1
8
4 1
2
4 2
9
1
3
All Pairs Shortest Path: Floyd-Warshall
class AllPairShortestPath {
int i, j, k;
} } }
printSolution(dist);
}
All Pairs Shortest Path: Floyd-Warshall
The running time of the algorithm is computed as :
End Of UNIT-5