Data Structures: Unit III - Part 4 Bca, NGMC

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Data Structures

Unit III – Part 4


BCA, NGMC
Dijkstra's algorithm
• Dijkstra's algorithm (or Dijkstra's Shortest
Path First algorithm, SPF algorithm) is an 
algorithm for finding the shortest paths
 between nodes in a graph, which may
represent, for example, road networks. It was
conceived by computer scientist Edsger W.
Dijkstra in 1956.
• This algorithm is used to find the shortest path
between two given nodes.
•Dijkstra's algorithm to
find the shortest path
between a and b.
•It picks the unvisited
vertex with the low
distance, calculates the
distance through it to
each unvisited neighbor,
and updates the
neighbor's distance if
smaller.
•Mark visited (set to red)
when done with
neighbors.
Minimum Spanning Tree (MST)
• A minimum spanning tree (MST) or minimum
weight spanning tree is a subset of the edges
of a connected, edge-weighted undirected
graph that connects all the vertices together,
without any cycles and with the minimum
possible total edge weight.
• That is, it is a spanning tree whose sum of
edge weights is as small as possible.
Kruskal's algorithm
• Kruskal's algorithm is a 
minimum-spanning-tree algorithm which finds
an edge of the least possible weight that
connects any two trees in the forest.
• It is a greedy algorithm in graph theory as it
finds a minimum spanning tree for a 
connected weighted graph adding increasing
cost arcs at each step.
Method
AD and CE are the shortest edges,
with length 5, and AD has been 
arbitrarily chosen, so it is highlighted.

CE is now the shortest edge that


does not form a cycle, with length
5, so it is highlighted as the second
edge.
The next edge, DF with length 6, is
highlighted using much the same
method.

The next-shortest edges are AB and BE,


both with length 7. AB is chosen
arbitrarily, and is highlighted. The
edge BD has been highlighted in red,
because there already exists a path (in
green) between B and D, so it would form
a cycle (ABD) if it were chosen.
The process continues to highlight
the next-smallest edge, BE with
length 7. Many more edges are
highlighted in red at this
stage: BC because it would form the
loop BCE, DE because it would form
the loop DEBA, and FE because it
would form FEBAD.

Finally, the process finishes with the


edge EG of length 9, and the
minimum spanning tree is found.
Prime’s Algorithm
• Prim's algorithm is a minimum spanning tree algorithm that
takes a graph as input and finds the subset of the edges of
that graph which
– form a tree that includes every vertex
– has the minimum sum of weights among all the trees that can be
formed from the graph
• Like Kruskal’s algorithm, Prim’s algorithm is also a 
Greedy algorithm.
• The algorithm was developed in 1930
by Czech mathematician Vojtech Jarnik and later
rediscovered and republished by computer scientists 
Robert C. Prim in 1957.
Algorithm
1. Initialize a tree with a single vertex, chosen
arbitrarily from the graph.
2. Grow the tree by one edge: of the edges that
connect the tree to vertices not yet in the
tree, find the minimum-weight edge, and
transfer it to the tree.
3. Repeat step 2 (until all vertices are in the
tree).

You might also like