Graph
Graph
Introduction
• A graph is a non-linear kind of data structure made up of nodes or vertices and
edges.
• The edges connect any two nodes in the graph, and the nodes are also known as
vertices.
• Directed Graph :
• A directed graph also referred to as a digraph, is a set of nodes connected
by edges, each with a direction.
Types Of Graph
• Disconnected Graph:
• When there is no edge linking the vertices, you refer to the null
graph as a disconnected graph.
•
• Cyclic Graph :
• If a graph contains at least one graph cycle, it is considered to be
cyclic.
Types Of Graph
Acyclic Graph :
• When there are no cycles in a graph, it is called an acyclic graph.
• There is a chance that there will be more than one minimum spanning
tree if there are numerous edges with the same weight.
Application of Spanning Tree
• Telecommunication Network Building:
Application of Spanning Tree
• Constructing Highways or Railroads :
• Basically, the algorithm treats the cities as the vertices and paths
joining them as edges to create a subtree that will make the route fully
connected and cost efficient.
Application of Spanning Tree
• Image Segmentation:
Dijkstra’s Algorithm
• Edsger W.Dijkstra invented Dijkstra’s algorithm to find the shortest
path for the required node between multiple nodes in the graph to
design a shortest-path tree.
• Dijkstra’s algorithm is used to find the shortest path between the two
mentioned vertices of a graph by applying the Greedy Algorithm as
the basis of principle.
• For Example: Used to find the shortest between the destination to
visit from your current location on a Google map.
Dijkstra’s Algorithm
• 1. We will find the shortest path from node A to the other nodes in the graph,
assuming that node A is the source.
• 2. For node B, apply the above-discussed algorithm, then on applying values:
if 0 + 20 < ∞
>[TRUE]
Node A to Node B = 20
• 3. For node C, on applying the approach from node A, we have:
if 0 + 50 < ∞
>[TRUE]
Node A to Node B = 50
• 4. For node C, from node B, on applying the algorithm, we have:
if 20 + 10 < ∞
>[TRUE]
Node B to Node C = 30
• 5. By the value obtained from step 3, we change the shortest distance between
node A to Node C to 30 from the previous distance of 50.
Dijkstra’s Example
Dijkstra’s solution
1. All the distances from node A to the rest of the nodes is ∞.
2. Calculating the distance between node A and the immediate nodes
(node B & node D).
3. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node B. Calculating the distance between
node B and the immediate nodes.
4. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node D. Calculating the distance between
node D and the immediate nodes.
5. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node E. Calculating the distance between
node E and the immediate nodes.
6. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node F. Calculating the distance between
node F and the immediate nodes.
What Is the Bellman-Ford Algorithm?
• Dynamic Programming is used in the Bellman-Ford algorithm.
• It begins with a starting vertex and calculates the distances between
other vertices that a single edge can reach.
• It then searches for a path with two edges, and so on. The Bellman-
Ford algorithm uses the bottom-up approach.
• Based on the "Principle of Relaxation," more accurate values
gradually recovered an approximation to the proper distance until
finally reaching the optimum solution.
• Negative weight edges can generate negative weight cycles, which
reduce the total path distance by returning to the same point.
What Is the Bellman-Ford Algorithm?
• Step 1: Make a list of all the graph's edges. This is simple if an
adjacency list represents the graph.
• Step 2: "V - 1" is used to calculate the number of iterations. Because
the shortest distance to an edge can be adjusted V - 1 time at most,
the number of iterations will increase the same number of vertices.
• Step 3: Begin with an arbitrary vertex and a minimum distance of
zero. Because you are exaggerating the actual distances, all other
nodes should be assigned infinity.
For each edge u-v, relax the path lengths for the vertices:
If distance[v] is greater than distance[u] + edge weight uv, then
distance[v] = distance[u] + edge weight uv
• Step 4:If the new distance is less than the previous one, update the
distance for each Edge in each iteration. The distance to each node
is the total distance from the starting node to this specific node.
• Step 5: To ensure that all possible paths are considered, you must
consider alliterations. You will end up with the shortest distance if
you do this.
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
Prim’s Algorithm
• Prim’s algorithm is used to find the Minimum Spanning Tree for a
given graph.
• Prim's approach identifies the subset of edges that includes every
vertex in the graph, and allows the sum of the edge weights to be
minimized.
• Prim’s algorithm starts with a single node and works its way
through several adjacent nodes, exploring all of the connected edges
along the way.
• Edges with the minimum weights that do not cause cycles in the
graph get selected for t inclusion in the MST structure. That is why it
is also known as a Greedy Algorithm.
How to Create MST Using Prim’s Algorithm
• Step 1: Determine the arbitrary starting vertex.
• Step 2: Keep repeating steps 3 and 4 until the fringe vertices (vertices
not included in MST)remain.
• Step 3: Select an edge connecting the tree vertex and fringe vertex
having the minimum weight.
• Step 4: Add the chosen edge to MST if it doesn’t form any closed cycle.
• Step 5: Exit
• After the inclusion of node A, you will look into the connected edges
going outward from node A and you will pick the one with a minimum
edge weight to include it in your T(V’, E’) structure.
How to Create MST Using Prim’s Algorithm
• Now, you have reached node B. From node B, there are two possible
edges out of which edge BD has the least edge weight value. So, you will
include it in your MST.
• From node D, you only have one edge. So, you will include it in your
MST. Further, you have node H, for which you have two incident edges.
Out of those two edges, edge HI has the least cost, so you will include it
in MST structure.
How to Create MST Using Prim’s Algorithm
• Similarly, the inclusion of nodes G and E will happen in MST.
• After that, nodes E and C will get included. Now, from node C, you have
two incident edges. Edge CA has the tiniest edge weight. But its
inclusion will create a cycle in a tree structure, which you cannot allow.
How to Create MST Using Prim’s Algorithm
• And we will include edge CF in this minimum spanning tree structure.
The summation of all the edge weights in MST T(V’, E’) is equal to 30,
which is the least possible edge weight for any possible spanning tree
structure for this particular graph.
Example MST Using Prim’s Algorithm
Solution MST Using Prim’s Algorithm