diff --git a/floyd_warshell_algorithm.cpp b/floyd_warshell_algorithm.cpp index c7f951e..f3ae807 100644 --- a/floyd_warshell_algorithm.cpp +++ b/floyd_warshell_algorithm.cpp @@ -1,5 +1,10 @@ /* AABRA KA DABRA.. !! + Here's how the Floyd-Warshall algorithm works: + +Initialization: Create a matrix, often called the "distance matrix" or "adjacency matrix," where each element dist[i][j] represents the shortest distance from vertex i to vertex j. Initially, this matrix is filled with the direct edge weights if there's an edge between vertices i and j, and it's filled with a special value (often infinity) for all other pairs. +Main Loop: The algorithm proceeds by considering all vertices one by one as intermediate vertices. For each vertex k, it checks if the path from vertex i to vertex j through vertex k is shorter than the current known distance from i to j. If it is, then the matrix is updated to reflect this shorter path. +Iteration: The algorithm repeats the Main Loop for all vertices, gradually refining the distance matrix until all shortest paths are found. */ #include "bits/stdc++.h" using namespace std; diff --git a/kruskal_min_spanning_tree.cpp b/kruskal_min_spanning_tree.cpp index 495f138..88343d9 100644 --- a/kruskal_min_spanning_tree.cpp +++ b/kruskal_min_spanning_tree.cpp @@ -1,15 +1,12 @@ -/* - AABRA KA DABRA.. !! -*/ - /** -A spanning tree is a sub-graph of an undirected connected graph, which includes all the vertices of the graph with a minimum possible number of edges. If a vertex is missed, then it is not a spanning tree. - -The edges may or may not have weights assigned to them. - -The total number of spanning trees with n vertices that can be created from a complete graph is equal to n(n-2). +Sort the Edges: First, sort all the edges of the graph in non-decreasing order of their weights. This can be done using any sorting algorithm, such as quicksort or mergesort. +Create an Empty Tree: Initially, the MST is an empty set of edges. +Iterate Over Edges: Begin iterating through the sorted edges, from the smallest to the largest weight. For each edge, do the following: +a. Check for a Cycle: If adding the current edge to the MST would create a cycle (i.e., if the two endpoints of the edge are already connected in the MST), skip this edge and move on to the next one. You can check for a cycle using techniques like disjoint-set data structures (e.g., Union-Find). +b. Add to MST: If the edge doesn't create a cycle, add it to the MST. This edge is now considered part of the MST. +Repeat: Continue this process until you have added V - 1 edges to the MST, where V is the number of vertices in the graph. This ensures that the resulting tree is indeed a spanning tree, connecting all vertices. +MST is Complete: At the end of the algorithm, you will have constructed a Minimum Spanning Tree that contains all the vertices of the original graph with the minimum possible total weight. -If we have n = 4, the maximum number of possible spanning trees is equal to 44-2 = 16. Thus, 16 spanning trees can be formed from a complete graph with 4 vertices. **/ #include "bits/stdc++.h" using namespace std;