Home Week-4 Week-6

PDSA - Week 5

 

Weighted Graph

Weighted directed graph

Adjacency matrix representation in Python

 

Adjacency list representation in Python

 

 

Weighted undirected graph

Adjacency matrix representation in Python

 

Adjacency list representation in Python

 


 

Shortest Path

Single source shortest path algorithm

Find shortest paths from a source vertex to every other vertex.

Dijkstra's Algorithm

Dijkstra's algorithm is a popular algorithm for finding the shortest path between two vertices in a weighted graph. Some of its important properties and points are:

  1. Single-source shortest path: Dijkstra's algorithm finds the shortest path from a single source vertex to all other vertices in the graph.

  2. Weighted graph: Dijkstra's algorithm only works on weighted graphs, where each edge has a weight or cost associated with it.

  3. Non-negative weights: Dijkstra's algorithm can only be used on graphs with non-negative edge weights. If the graph contains negative weights, Bellman-Ford algorithm is a better choice.

  4. Greedy algorithm: Dijkstra's algorithm is a greedy algorithm that selects the vertex with the smallest distance from the starting vertex and explores its neighbors. This process is repeated until the shortest path to all vertices is found.

  5. Optimal substructure: Dijkstra's algorithm relies on the optimal substructure property, which means that the optimal solution to a problem can be obtained by combining the optimal solutions to its subproblems.

  6. Can handle disconnected graphs: Dijkstra's algorithm can handle disconnected graphs, but it will only find the shortest path for the connected component containing the source vertex.

 

Algorithm Steps

Here are the steps for Dijkstra's algorithm for finding the single source shortest path:

  1. Create a table to store the distances from the source vertex to each vertex in the graph. Initialize the source vertex with a distance of 0 and all other vertices with infinity. Also create a set of unvisited vertices and mark all vertices as unvisited.

  2. While the set of unvisited vertices is not empty, do the following:

  3. Select the unvisited vertex with the smallest distance from the source vertex. This vertex is now considered visited.

  4. For each neighbor of the visited vertex that is still unvisited, calculate the distance to that neighbor by adding the weight of the edge between the visited vertex and the neighbor to the distance of the visited vertex. If this distance is smaller than the distance currently stored for the neighbor in the table, update the table with the new distance.

  5. After updating the distances for all neighbors, mark the visited vertex as visited.

  6. Repeat steps 3 to 5 until all vertices have been visited or the destination vertex has been visited.

  7. Once the algorithm has visited all vertices, the table will contain the shortest distances from the source vertex to each vertex in the graph.

  8. To find the shortest path from the source vertex to a destination vertex, backtrack from the destination vertex to the source vertex by following the path with the smallest distance at each step. This will give you the shortest path from the source vertex to the destination vertex.

 

Working Visualization

Select Dijkstra(s) form left bottom menu

 

For given graph

 

Implementation Dijkstra's For Adjacency matrix

Output

Complexity

O(V2)

 

Implementation Dijkstra's For Adjacency List

Output

Code Execution Flow

 

Complexity

O(V2)

 


 

Bellman Ford algorithm

Bellman-Ford algorithm is a dynamic programming based algorithm to find the shortest path in a weighted graph, where the edge weights may be negative. Here are some important points and properties of the algorithm:

  1. It can handle graphs with negative edge weights: Unlike Dijkstra's algorithm, which can only handle non-negative edge weights, the Bellman-Ford algorithm can handle graphs with negative edge weights. However, it cannot handle graphs with negative cycles, which are cycles that have a negative sum of edge weights.

  2. It can detect negative cycles: The Bellman-Ford algorithm can detect negative cycles in a graph. If there is a negative cycle, the algorithm will report that there is no shortest path from the source vertex to some other vertex.

  3. It can handle disconnected graphs: The Bellman-Ford algorithm can handle disconnected graphs by finding the shortest paths from the source vertex to all other vertices in each connected component.

  4. It uses dynamic programming: The Bellman-Ford algorithm uses dynamic programming to solve the shortest path problem. It maintains an array of distances that is gradually updated until it converges to the correct values.

  5. Bellman-Ford works for both directed and undirected graphs with non-negative edges weights.

 

Algorithm Steps

Here are the step-by-step instructions for the Bellman-Ford algorithm:

  1. Initialize the distance array: Set the distance of the source vertex to 0 and the distances of all other vertices to infinity.

  2. Relax all edges: Repeat the following step (V-1) times, where V is the number of vertices in the graph. For each edge (u,v) with weight w, if the distance to u plus w is less than the distance to v, update the distance to v to the distance to u plus w.

  3. Check for negative weight cycles: After relaxing all edges V-1 times, check for negative weight cycles. For each edge (u,v) with weight w, if the distance to u plus w is less than the distance to v, there exists a negative weight cycle.

  4. Print the distance array: If there are no negative weight cycles, print the distance array, which contains the shortest path distances from the source vertex to all other vertices.

 

Working Visualization

Select BellmanFord(s) form left bottom menu

https://visualgo.net/en/sssp

 

For given graph

Implementation of Bellman Ford for adjacency matrix

Output

Complexity

O(V3) - where V is number of vertices.

 

Implementation of Bellman Ford for adjacency list

Output

 

Code Execution Flow

 

Complexity

O(VE)- where E is number of edges and V is number of vertices.

 


 

All pair of shortest path

Floyd-Warshall algorithm

The Floyd-Warshall algorithm is an efficient algorithm for finding the shortest path between all pairs of nodes in a weighted graph. Some important points and properties of the Floyd-Warshall algorithm are:

  1. All-pairs shortest path: The Floyd-Warshall algorithm computes the shortest path between all pairs of nodes in the graph.

  2. Negative cycles: The Floyd-Warshall algorithm can detect negative cycles in the graph. A negative cycle is a cycle in the graph where the sum of the weights of the edges is negative. If there is a negative cycle in the graph, then the algorithm will report that there is a negative cycle.

  3. Dynamic programming: The Floyd-Warshall algorithm uses dynamic programming to compute the shortest path between all pairs of nodes in the graph. The algorithm builds a table of shortest paths between pairs of nodes by iteratively considering intermediate nodes along the path.

  4. No guarantee of uniqueness: The shortest path between two nodes in a graph may not be unique. If there are multiple shortest paths between two nodes, then the Floyd-Warshall algorithm may return any one of them.

  5. Floyd-Warshall's works for both directed and undirected graphs with non-negative edges weights.

  6. Floyd-Warshall's does not work with an undirected graph with negative edges weight, as it will be declared as a negative weight cycle.

 

Algorithm Steps

Here are the steps for the algorithm:

  1. Initialization: Create a 2-dimensional array SP of size n x n, where n is the number of vertices in the graph. For each pair of vertices (i,j), initialize SP[i][j] to the weight of the edge from vertex i to vertex j. If there is no edge between vertices i and j, then set SP[i][j] to infinity.

  2. For each vertex k from 1 to n, compute the shortest path between every pair of vertices (i,j) that passes through k. To do this, update SP[i][j] as follows:

    SP[i][j] = min(SP[i][j], SP[i][k] + SP[k][j])

    This means that the shortest path from vertex i to vertex j that passes through k is the minimum of the current shortest path from i to j and the sum of the shortest path from i to k and the shortest path from k to j.

  3. After the step 2 is complete, the SP array will contain the shortest path between every pair of vertices in the graph.

 

For given input graph

 

Implementation of Floyd Warshall Algorithm

Output

Complexity

O(V3) - where V is number of vertices.

 


 

Spanning Tree(ST)

Minimum Cost Spanning Tree(MCST)

Algorithms:-

 

Prim's Algorithm

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree (MST) for a weighted undirected graph. Here are some of the key points or properties of Prim's algorithm:

  1. Greedy approach: Prim's algorithm is a greedy algorithm that works by building up the MST one vertex at a time, always choosing the cheapest edge that connects the growing tree to an outside vertex.

  2. Works on connected graphs: Prim's algorithm only works on connected graphs, meaning that there must be a path between any two vertices in the graph.

  3. Weighted edges: Prim's algorithm requires that the graph have weighted edges. The weights represent the cost or distance of traveling between two vertices.

  4. Spanning tree: Prim's algorithm always produces a spanning tree, which is a subset of the edges that connects all vertices in the graph and contains no cycles.

  5. Unique solution: If the weights of the edges are unique, then the MST produced by Prim's algorithm is unique.

 

Algorithm Steps

Here are the steps for the algorithm:

  1. Choose any vertex as the starting vertex and add it to the MST set. Initialize the weight of this vertex to 0.

  2. Find the edge with the minimum weight that connects the starting vertex to any other vertex.

  3. Add the adjacent vertex connected by the minimum weight edge to the MST set. Set its weight to the weight of the edge.

  4. Repeat steps 2 and 3 until all vertices are included in the MST set.

 

Working Visualization

Select Prim's Algorithm from bottom-left menu

 

Source:- https://visualgo.net/en/mst

 

For given input graph

Implementation of Prim's Algorithm

Output

Output minimum spanning tree with cost 44

Code Execution Flow

 

 

Other Implementation

Output

 

Code Execution Flow

 

Complexity

O(V2) - where V is number of vertices.

 


 

Kruskal's Algorithm

Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a connected, undirected graph. The algorithm works by iteratively adding the edges of the graph with the smallest weights that do not create a cycle in the MST. Here are some important points and properties of Kruskal's algorithm:

  1. Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a connected, undirected graph.

  2. The algorithm works by iteratively adding the edges of the graph with the smallest weights that do not create a cycle in the MST.

  3. Kruskal's algorithm guarantees that the MST is unique if the graph has unique edge weights.

 

Algorithm Steps

Here are the steps for the of Kruskal's algorithm:

  1. Sort all the edges of the graph in non-decreasing order of their weights.

  2. Create a new empty set for the minimum spanning tree (MST).

  3. Iterate through the sorted edges, starting with the edge with the smallest weight.

  4. For each edge, check if adding it to the MST will create a cycle.

  5. If the edge does not create a cycle, add it to the MST.

  6. Repeat steps 4 and 5 until all vertices are included in the MST, or until the desired number of edges have been added.

  7. Return the MST.

 

Working Visualization

Select Kruskal's Algorithm from bottom-left menu

 

Source:- https://visualgo.net/en/mst

For given input graph

Implementation of Kruskal's Algorithm

Output

Output minimum spanning tree with cost 121

Code Execution Flow

 

Complexity

O(V2) - where V is number of vertices.