0% found this document useful (0 votes)
6 views

Prim and Dij

Prim's Algorithm is a greedy method for finding the minimum spanning tree of a graph by selecting edges with the smallest weights while avoiding cycles. The algorithm starts with a chosen vertex and iteratively adds the minimum weight edge connecting the tree to a new vertex until all vertices are included. Dijkstra's Algorithm, another greedy approach, finds the shortest path from a source vertex to all other vertices in a weighted graph with positive weights, utilizing a priority queue for efficiency.

Uploaded by

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

Prim and Dij

Prim's Algorithm is a greedy method for finding the minimum spanning tree of a graph by selecting edges with the smallest weights while avoiding cycles. The algorithm starts with a chosen vertex and iteratively adds the minimum weight edge connecting the tree to a new vertex until all vertices are included. Dijkstra's Algorithm, another greedy approach, finds the shortest path from a source vertex to all other vertices in a weighted graph with positive weights, utilizing a priority queue for efficiency.

Uploaded by

Nancy P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph.

Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the
weights of the edges can be minimized.

Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting
edges at every step. The edges with the minimal weights causing no cycles in the graph got selected.

How does the prim's algorithm work?

 Remove loops / Self Edges


 Remove parallel edges

Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the edges with the
smallest weight until the goal is reached. The steps to implement the prim's algorithm are given as follows
-

o First, we have to initialize an MST with the randomly chosen vertex.


o Now, we have to find all the edges that connect the tree in the above step with the new vertices.
From the edges found, select the minimum edge and add it to the tree.
o Repeat step 2 until the minimum spanning tree is formed.
The applications of prim's algorithm are -

o Prim's algorithm can be used in network designing.


o It can be used to make network cycles.
o It can also be used to lay down electrical wiring cables.

Example of prim's algorithm

Now, let's see the working of prim's algorithm using an example. It will be easier to understand the prim's
algorithm using an example.

Suppose, a weighted graph is -

Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.

Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges from
vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges, the edge BD
has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other
edges. In this case, the edges DE and CD are such edges. Add them to MST and
explore the adjacent of C, i.e., E and A. So, select the edge DE and add it to the MST.

Step 4 - Now, select the edge CD, and add it to the MST.

Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle to the
graph. So, choose the edge CA and add it to the MST.

So, the graph produced in step 5 is the minimum spanning tree of the given graph. The cost of the MST
is given below -

Cost of MST = 4 + 2 + 1 + 3 = 10 units.

Algorithm

1. Step 1: Select a starting vertex


2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT

o Time Complexity

Data structure used for the minimum edge weight Time Complexity

Adjacency matrix, linear searching O(|V|2)

Adjacency list and binary heap O(|E| log |V|)

Adjacency list and Fibonacci heap O(|E|+ |V| log |V|)


Dijkstra's Algorithm - Single Source Shortest Path
Dijkstra's Algorithm is a Graph algorithm that finds the shortest path from a source vertex to all other
vertices in the Graph (single source shortest path).
It is a type of Greedy Algorithm that only works on Weighted Graphs having positive weights.
This algorithm works for both directed and undirected graphs
It works only for connected graphs
The graph should not contain negative edge weights

A function Dijkstra(Graph, source):


dist[source] := 0 // Distance from source to source is set to 0
for each vertex v in Graph: // Initializations
if v ≠ source
dist[v] := infinity // Unknown distance function from source to each node set to infinity
add v to Q // All nodes initially in Q

while Q is not empty: // The main loop


u := vertex in Q with min dist[u] // In the first run-through, this vertex is the source node
remove v from Q
for each neighbor v of u: // where neighbor u has not yet been removed from Q.
alt := dist[u] + cost(u,v)
if alt < dist[v]: // A shorter path to u has been found
dist[v] := alt // Update distance of u
return dist[]
end function

The time complexity of Dijkstra's Algorithm is O(V2) with the help of the adjacency matrix
representation of the graph.
This time complexity can be reduced to O((V + E) log V) with the help of an adjacency list
representation of the graph, where V is the number of vertices and E is the number of edges in the
graph.
Steps Diagram
The set sptSet is initially empty and distances assigned
to vertices are {0, INF, INF, INF, INF, INF, INF, INF}
where INF indicates infinite.
Now pick the vertex with a minimum distance value.
The vertex 0 is picked, include it in sptSet .
So sptSet becomes {0}.
After including 0 to sptSet , update distance values of its
adjacent vertices.
Adjacent vertices of 0 are 1 and 7. The distance values of
1 and 7 are updated as 4 and 8.
Pick the vertex with minimum distance value and not
already included in SPT (not in sptSET ). The vertex 1
is picked and added to sptSet .
So sptSet now becomes {0, 1}. Update the distance
values of adjacent vertices of 1.
The distance value of vertex 2 becomes 12 .

Pick the vertex with minimum distance value and not


already included in SPT (not in sptSET ). Vertex 7 is
picked. So sptSet now becomes {0, 1, 7}.
Update the distance values of adjacent vertices of 7. The
distance value of vertex 6 and 8 becomes finite ( 15 and
9 respectively).

Pick the vertex with minimum distance value and not


already included in SPT (not in sptSET ). Vertex 6 is
picked. So sptSet now becomes {0, 1, 7, 6} .
Update the distance values of adjacent vertices of 6. The
distance value of vertex 5 and 8 are updated.
We repeat the above steps until sptSet includes all
vertices of the given graph. Finally, we get the following
S hortest Path Tree (SPT).

You might also like