Unit 4 Graphs
Unit 4 Graphs
Introduction
A graph G is defined as an ordered set (V, E), where V(G) represents the set of vertices and E(G)
represents the edges that connect these vertices.
Closed path A path P is known as a closed path if the edge has the same end-points. That
is, if v0 = vn.
Simple path A path P is known as a simple path if all the nodes in the path are distinct.
Cycle A closed simple path with length 3 or more. A cycle of length k is known as k-cycle.
Connected graph A graph is said to be connected if for any two vertices (u, v) in V there is
a path from u to v. That is to say that there are no isolated nodes in a connected graph. A
connected graph that does not have any cycle is called a tree/ free tree/ tree graph .
Complete graph A graph is said to be complete ,
there is an edge between every single pair of the
vertices in the graph. A complete graph is connected.
A complete graph has n(n–1)/2 edges, where n is the
number of nodes in G.
Labelled graph or weighted graph A graph is said
to be labelled if every edge in the graph is
assigned some data. In a weighted graph, the
edges of the graph are assigned some non-
negative weight or length.
Multiple edges Distinct edges which connect the same end-points are
called multiple
edges. That is, e = (u, v) and e' = (u, v) are known as multiple edges of G.
Loop An edge that has identical end-points is called a loop. That is, e = (u,
u). Loop constitute degree 2
Reachability A node v is said to be reachable from node u, if and only if there exists a
(directed) path from node u to node v.
that we will begin the traversal of the following graph from vertex 2 .
For the given graph, there are two different BFS traversals: 2,3,0,1
and 2,0,3,1
• Once, the queue becomes empty, stop the BFS process .
Output of BFS is the order in which elements are
dequeued.
Find path P from A BFS
to I
• DFS :S A D B C
DFS
Consider the graph G given in Fig.
13.23. The adjacency list of G is
also given.
Suppose we want to print all the
nodes that can be reached from the
node H (including H itself).
Kruskal’s algorithm sorts all the edges in increasing order of their edge
weights and keeps adding nodes to the tree only if the chosen edge
does not form any cycle.
Also, it picks the edge with a minimum cost at first and the edge with a
maximum cost at last.
Hence, you can say that the Kruskal algorithm makes a locally optimal
choice, intending to find the global optimal solution. That is why it is called a
Greedy Algorithm.
Algo
• Step 1: Sort all edges in increasing order of
their edge weights.
• Step 2: Pick the smallest edge.
• Step 3: Check if the new edge creates a cycle
or loop in a spanning tree.
• Step 4: If it doesn’t form the cycle, then include
that edge in MST. Otherwise, discard it.
• Step 5: Repeat from step 2 until it includes |V| -
1 edges in MST.
Implement Kruskal Algo
1. Proceed with arranging all edges in a sorted list by their
edge weights.
The running time of Kruskal’s and Prims algorithm can be given as
O(E log V), where E is the number of edges and V is the number of
vertices in the graph.
Consider the graph given below.
Find the minimum spanning tree of
this graph using (a) Prim’s
algorithm, (b) Kruskal’s algorithm,
Dijkstra's Algorithm
• Purpose and Use Cases
• With Dijkstra's Algorithm, you can find the shortest path between nodes
in a graph.
• Particularly, you can find the shortest path from a node (called the
"source node") to all other nodes in the graph, producing a shortest-
path tree.
• This algorithm is used in GPS devices to find the shortest path between
the current location and the destination.
• It has broad applications in industry, specially in domains that require
modeling networks.
• Algorithm
•
Now let’s see the algorithm step by step:
• 1. First of all, we will mark all vertex as unvisited vertex
2. Then, we will mark the source vertex as 0 and all other vertices as infinity
3. Consider source vertex as current vertex
4. Calculate the path length of all the neighboring vertex from the current vertex by adding the
weight of the edge in the current vertex
5. Now, if the new path length is smaller than the previous path length then replace it
otherwise ignore it
6. Mark the current vertex as visited after visiting the neighbor vertex of the current vertex
7. Select the vertex with the smallest path length as the new current vertex and go back to
step 4.
8. Repeat this process until all the vertex are marked as visited.
• Example of Dijkstra's Algorithm
• Now that you know more about this algorithm, let's see how it works
behind the scenes with a a step-by-step example. The algorithm will
generate the shortest path from node 0 to all the other nodes in
the graph.
• 💡 Tip: For this graph, we will assume that the weight of the edges
represents the distance between two nodes.
• Initially, we have this list of distances (please
see the list below):
• The distance from the source node to itself
is 0. For this example, the source node will be
node 0 but it can be any node that you choose.
• The distance from the source node to all other
nodes has not been determined yet, so we use
the infinity symbol to represent this initially.
• We also have this list to keep track of the
nodes that have not been visited yet (nodes
that have not been included in the path)
• Tip: Remember that the algorithm is
completed once all nodes have been added
to the path.
Refer this tutorial
• https://www.freecodecamp.org/news/dijkstras-shortest-
path-algorithm-visual-introduction/
Step 1: We begin with the list of visited vertices empty and all the nodes in the list of
unvisited vertices. The distance from A to A is 0. The shortest distance to all other
vertices is infinity. The current vertex is set to A.
• Step 2: A’s unvisited neighbors are B and
D. The distance to B and D through A
are 0 + 12 = 12 and 0 + 2 = 2
respectively.
• These are shorter than infinity and thus
set to the new shortest distances. A is
added to the list of visited vertices.
• Both distances change; therefore, A is
set to the previous vertex for B and D. D
has the shortest distance and is set
to the next current node.
• Step 3: D’s unvisited neighbors are B and
E. The distance to B and E through D are
2 + 4= 6 and 2 + 2 = 4 respectively.
• The B node distance is changed to 6, and
E’s distance is changed to 4 as both are
lower distances. D is added to the list of
visited vertices and as the previous
vertex for B and E. The new shortest
distance for the D’s neighbors is 4. E is
now the new current node.
• Step 4: E’s
unvisited neighbors
are B and C. The
distance to B and C
through E are 4+
4= 8 and 4+ 10=
14, respectively.
• The distance to C is
changed to 14. E is
added as C’s
previous vertex.
The new shortest
distance for the E’s
neighbors is 6. B is
now the new
current node.
• B’s only remaining
unvisited neighbor is C.
The distance to C
through B is 6+ 10=
16. Nothing is changed.
C is set to the current
node. However, it has
no unvisited vertice
neighbors left, so the
algorithm returns the
table information and
ceases.
Floyd Warshall algorithm
• Shortest path algorithm, similar to Dijkstra’s algorithm
and Bellman ford’s algorithm.
• The only difference is Dijkstra’s and Bellman Ford’s
algorithms are single source shortest path algorithms,
whereas Floyd Warshall is all pair shortest path algorithm,
it can compute the shortest path between every pair of
vertices in the graph.
Floyd Warshall Algorithm
Algorithm:
• Initialize the solution matrix same as the input graph matrix as a first step.
• Then update the solution matrix by considering all vertices as an intermediate vertex.
• The idea is to pick all vertices one by one and updates all shortest paths which include
the picked vertex as an intermediate vertex in the shortest path.
• When we pick vertex number k as an intermediate vertex, we already have considered
vertices {0, 1, 2, .. k-1} as intermediate vertices.
• For every pair (i, j) of the source and destination vertices respectively, there are two
possible cases.
• k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is.
• k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i]
[k] + dist[k][j], if dist[i][j] > dist[i][k] + dist[k][j]
Now calculating the Distance of
every pair Via A.
Now calculating the Distance of every pair Via B and updating if the
new found weight is lesser than the mentioned weight in the matrix
DB =
Now, calculating distance of each path via C Similarly, if we
calculate the shortest distance Via D, No changes will be
required. and D
• Complexity
• Following are the complexities in the algorithm:
• Time Complexity: There are three for loops in the pseudo-code of the algorithm, so
the time complexity will be O (n^3).
• Space Complexity: The space complexity of Floyd’s Warshall algorithm is O (n^2).
• Application
• In networking devices
• In Routing data packets
• Calculate the inversion of the real matrix
• Calculating transitive closure of directed graphs
Warshall’s Algorithm
END