Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1 Kruskal’s Algorithm

Kruskal’s algorithm is a greedy algorithm that finds a minimum spanning tree


for a connected weighted graph. The algorithm works by sorting the edges in
the graph by their weights, and then adding the edges to the minimum spanning
tree in order of increasing weight, as long as adding the edge does not create a
cycle.
Here’s the pseudocode for Kruskal’s algorithm:

Algorithm 1 Kruskal’s algorithm


1: Sort the edges in the graph by weight.
2: Initialize an empty set for the minimum spanning tree.
3: for each edge in the sorted edges do
4: if adding the edge does not create a cycle then
5: Add the edge to the minimum spanning tree.
6: end if
7: end for

2 Sollin’s Algorithm
Sollin’s algorithm is another greedy algorithm that finds a minimum spanning
tree for a connected weighted graph. The algorithm works by maintaining a
forest of trees, and iteratively adding the lightest edge that connects each tree
to another tree, until all the trees are connected.
Here’s the pseudocode for Sollin’s algorithm:

Algorithm 2 Sollin’s algorithm


1: Initialize a forest of trees, where each vertex is a separate tree.
2: while there is more than one tree in the forest do
3: For each tree in the forest, find the lightest edge that connects it to another
tree.
4: Add the lightest edges to the minimum spanning tree and merge the trees.
5: end while

3 Dijkstra’s Algorithm
Dijkstra’s algorithm is a shortest path algorithm that finds the shortest path
between a source vertex and all other vertices in a weighted graph. The algo-
rithm works by maintaining a priority queue of vertices and their distances from
the source vertex, and iteratively selecting the vertex with the smallest distance
and updating the distances of its neighbors.
Here’s the pseudocode for Dijkstra’s algorithm:

1
Algorithm 3 Dijkstra’s algorithm
1: Initialize a priority queue of vertices, with the source vertex having distance
0 and all other vertices having distance infinity.
2: while the priority queue is not empty do
3: Select the vertex with the smallest distance from the priority queue.
4: for each neighbor of the selected vertex do
5: Calculate the distance from the source vertex to the neighbor through
the selected vertex.
6: if the calculated distance is smaller than the current distance of the
neighbor then
7: Update the distance of the neighbor in the priority queue.
8: end if
9: end for
10: end while

You might also like