This document discusses algorithms for finding minimum spanning trees and shortest paths in graphs. It covers Prim's algorithm and Kruskal's algorithm for finding minimum spanning trees, and Dijkstra's algorithm for finding single-source shortest paths in graphs with non-negative edge weights. Examples are provided to illustrate how each algorithm works on sample graphs by progressively building up the minimum spanning tree or shortest path tree. Resources for further learning about data structures and algorithms are also listed.
The document discusses minimum spanning tree algorithms for finding low-cost connections between nodes in a graph. It describes Kruskal's algorithm and Prim's algorithm, both greedy approaches. Kruskal's algorithm works by sorting edges by weight and sequentially adding edges that do not create cycles. Prim's algorithm starts from one node and sequentially connects the closest available node. Both algorithms run in O(ElogV) time, where E is the number of edges and V is the number of vertices. The document provides examples to illustrate the application of the algorithms.
The document discusses minimum spanning trees (MSTs) and algorithms for finding them. It defines an MST as a subgraph of an undirected weighted graph that spans all nodes, is connected, acyclic, and has the minimum total edge weight among all spanning trees. The document explains Prim's and Kruskal's algorithms for finding MSTs and provides examples of how they work on sample graphs. It also discusses properties of MSTs such as that multiple MSTs may exist for a given graph.
The document discusses minimum spanning trees and algorithms for finding them. It defines a minimum spanning tree as a connected subgraph of edges from a graph that connects all nodes with the minimum total edge weight. Two common algorithms are described: Kruskal's algorithm, which works by sequentially adding the shortest edges that do not create cycles; and Prim's algorithm, which grows a tree by sequentially adding the shortest edge connected to the growing tree. Both algorithms are greedy approaches that run in O(ElogV) time where E is edges and V is vertices. The document provides examples of applying each algorithm to find the minimum spanning tree of sample graphs.
Shortest path by using suitable algorithm.pdfzefergaming
This document discusses several algorithms for finding shortest paths in graphs, including Prim's algorithm, Kruskal's algorithm, Dijkstra's algorithm, and Bellman-Ford algorithm. It provides examples of applying Prim's algorithm to find the minimum spanning tree of a weighted undirected graph. It also summarizes Kruskal's algorithm as choosing the edge with the least weight without creating cycles until a spanning tree is formed. Overall, the document covers shortest path algorithms, their applications, and provides examples of Prim's and Kruskal's algorithm.
- A minimum spanning tree (MST) connects all nodes in a graph with the minimum total edge weight while avoiding cycles.
- There are different algorithms that can find an MST, such as Kruskal's algorithm and Prim's algorithm, which were introduced in the document.
- Kruskal's algorithm works by sorting the edges by weight and building the MST by adding the shortest edges that do not create cycles. Prim's algorithm grows the MST from an initial node by always adding the shortest edge connected to the current MST.
Kruskal's and Prim's algorithms can both be used to find a minimum spanning tree for a graph. Kruskal's algorithm considers edges and selects the lowest weight edge that does not form a cycle at each step. Prim's algorithm considers vertices and selects the lowest weight edge connecting a vertex already in the tree to one not yet in the tree. Dijkstra's algorithm can find the shortest path between two vertices in a graph by iteratively assigning final labels to vertices starting from the source vertex.
This document summarizes several graph algorithms, including:
1) Prim's algorithm for finding minimum spanning trees, which grows a minimum spanning tree by successively adding the closest vertex;
2) Dijkstra's algorithm for single-source shortest paths, which is similar to Prim's and finds shortest paths from a source vertex to all others;
3) An algorithm for all-pairs shortest paths based on repeated squaring of the weighted adjacency matrix, with multiplication replaced by minimization.
This document describes Sollin's algorithm, also known as Boruvka's algorithm, for finding a minimum spanning tree (MST) of a connected, edge-weighted undirected graph. Sollin's algorithm is a greedy algorithm that works by repeatedly contracting edges of minimum weight to form subgraphs until a single vertex remains, resulting in an MST. The algorithm proceeds by first highlighting the cheapest outgoing edge for each vertex, then contracting edges to form subgraphs and repeating on each subgraph until an MST is produced. An example applying the algorithm to a graph is provided.
The document discusses minimum spanning tree algorithms for finding low-cost connections between nodes in a graph. It describes Kruskal's algorithm and Prim's algorithm, both greedy approaches. Kruskal's algorithm works by sorting edges by weight and sequentially adding edges that do not create cycles. Prim's algorithm starts from one node and sequentially connects the closest available node. Both algorithms run in O(ElogV) time, where E is the number of edges and V is the number of vertices. The document provides examples to illustrate the application of the algorithms.
The document discusses minimum spanning trees (MSTs) and algorithms for finding them. It defines an MST as a subgraph of an undirected weighted graph that spans all nodes, is connected, acyclic, and has the minimum total edge weight among all spanning trees. The document explains Prim's and Kruskal's algorithms for finding MSTs and provides examples of how they work on sample graphs. It also discusses properties of MSTs such as that multiple MSTs may exist for a given graph.
The document discusses minimum spanning trees and algorithms for finding them. It defines a minimum spanning tree as a connected subgraph of edges from a graph that connects all nodes with the minimum total edge weight. Two common algorithms are described: Kruskal's algorithm, which works by sequentially adding the shortest edges that do not create cycles; and Prim's algorithm, which grows a tree by sequentially adding the shortest edge connected to the growing tree. Both algorithms are greedy approaches that run in O(ElogV) time where E is edges and V is vertices. The document provides examples of applying each algorithm to find the minimum spanning tree of sample graphs.
Shortest path by using suitable algorithm.pdfzefergaming
This document discusses several algorithms for finding shortest paths in graphs, including Prim's algorithm, Kruskal's algorithm, Dijkstra's algorithm, and Bellman-Ford algorithm. It provides examples of applying Prim's algorithm to find the minimum spanning tree of a weighted undirected graph. It also summarizes Kruskal's algorithm as choosing the edge with the least weight without creating cycles until a spanning tree is formed. Overall, the document covers shortest path algorithms, their applications, and provides examples of Prim's and Kruskal's algorithm.
- A minimum spanning tree (MST) connects all nodes in a graph with the minimum total edge weight while avoiding cycles.
- There are different algorithms that can find an MST, such as Kruskal's algorithm and Prim's algorithm, which were introduced in the document.
- Kruskal's algorithm works by sorting the edges by weight and building the MST by adding the shortest edges that do not create cycles. Prim's algorithm grows the MST from an initial node by always adding the shortest edge connected to the current MST.
Kruskal's and Prim's algorithms can both be used to find a minimum spanning tree for a graph. Kruskal's algorithm considers edges and selects the lowest weight edge that does not form a cycle at each step. Prim's algorithm considers vertices and selects the lowest weight edge connecting a vertex already in the tree to one not yet in the tree. Dijkstra's algorithm can find the shortest path between two vertices in a graph by iteratively assigning final labels to vertices starting from the source vertex.
This document summarizes several graph algorithms, including:
1) Prim's algorithm for finding minimum spanning trees, which grows a minimum spanning tree by successively adding the closest vertex;
2) Dijkstra's algorithm for single-source shortest paths, which is similar to Prim's and finds shortest paths from a source vertex to all others;
3) An algorithm for all-pairs shortest paths based on repeated squaring of the weighted adjacency matrix, with multiplication replaced by minimization.
This document describes Sollin's algorithm, also known as Boruvka's algorithm, for finding a minimum spanning tree (MST) of a connected, edge-weighted undirected graph. Sollin's algorithm is a greedy algorithm that works by repeatedly contracting edges of minimum weight to form subgraphs until a single vertex remains, resulting in an MST. The algorithm proceeds by first highlighting the cheapest outgoing edge for each vertex, then contracting edges to form subgraphs and repeating on each subgraph until an MST is produced. An example applying the algorithm to a graph is provided.
This document describes algorithms for finding minimum spanning trees in graphs. It discusses Prim's algorithm and Kruskal's algorithm, which are greedy algorithms used to find minimum spanning trees in undirected weighted graphs. It compares the time complexity of the two algorithms and notes that Prim's algorithm has lower time complexity and is simpler to implement. The document also outlines applications of minimum spanning trees and provides examples of applying Prim's and Kruskal's algorithms to find the minimum spanning tree of sample graphs.
This document discusses various algorithms for finding shortest paths in graphs, including Dijkstra's algorithm, breadth-first search (BFS), depth-first search (DFS), and Bellman-Ford algorithm. It provides pseudocode examples and explanations of how each algorithm works. It also covers properties of spanning trees, minimum spanning tree algorithms like Kruskal's and Prim's, and applications of spanning trees like network planning and routing protocols.
The document discusses greedy algorithms and their applications. It provides examples of problems that greedy algorithms can solve optimally, such as the change making problem and finding minimum spanning trees (MSTs). It also discusses problems where greedy algorithms provide approximations rather than optimal solutions, such as the traveling salesman problem. The document describes Prim's and Kruskal's algorithms for finding MSTs and Dijkstra's algorithm for solving single-source shortest path problems. It explains how these algorithms make locally optimal choices at each step in a greedy manner to build up global solutions.
This document discusses algorithms for solving the all-pairs shortest path problem in graphs. It defines the all-pairs shortest path problem as finding the shortest path between every pair of nodes in a graph. It then describes two main algorithms for solving this problem: Floyd-Warshall and Johnson's algorithm. Floyd-Warshall finds all-pairs shortest paths in O(n3) time using dynamic programming. Johnson's algorithm improves this to O(V2logV+VE) time by first transforming the graph to make edges positive, then running Dijkstra's algorithm from each node.
This document summarizes key concepts from a faculty development program on data structures, including graph applications, minimum spanning trees, shortest path algorithms, biconnected graphs, and Euler circuits. It provides examples and pseudocode for Prim's and Kruskal's minimum spanning tree algorithms and Dijkstra's shortest path algorithm. It also discusses identifying articulation points in a graph to determine if it is biconnected and conditions for the existence of Euler paths and circuits.
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMABIRAMIS87
Kruskal's algorithm is used to find the minimum spanning tree (MST) of a connected, undirected graph. It works by sorting the edges by weight and building the MST by adding edges one by one if they do not form cycles. The MST has the minimum total weight among all spanning trees of the graph. Ford-Fulkerson algorithm finds the maximum flow in a flow network and uses augmenting paths to incrementally increase the flow until no more augmenting paths exist. Dijkstra's algorithm solves the single-source shortest path problem to find the shortest paths from a source vertex to all other vertices in a weighted graph.
Dijkstra's algorithm is used to find the shortest paths from a single source vertex to all other vertices in a graph. It works by maintaining two sets - a visited set containing vertices whose shortest paths are known, and an unvisited set of remaining vertices. It iteratively selects the vertex in the unvisited set with the shortest path, relaxes its edges to update path lengths, and moves it to the visited set until all vertices are processed. An example application of Dijkstra's algorithm on a sample graph is provided to find the shortest paths from vertex S to all other vertices.
This document provides an overview of minimal spanning trees, including basic terminology, applications, and algorithms. It defines a minimal spanning tree as a spanning tree (subgraph containing all vertices and no cycles) with the minimum sum of edge weights. Common applications include phone network design and electronic circuitry wiring. The document describes Prim's and Kruskal's algorithms for finding a minimal spanning tree in a graph. Prim's is a vertex-based greedy algorithm that grows the tree one vertex at a time. Kruskal's is an edge-based algorithm that considers edges in order of weight, adding them if they connect different trees.
The document discusses randomized graph algorithms and techniques for analyzing them. It describes a linear time algorithm for finding minimum spanning trees (MST) that samples edges and uses Boruvka's algorithm and edge filtering. It also discusses Karger's algorithm for approximating the global minimum cut in near-linear time using edge contractions. Finally, it presents an approach for 3-approximate distance oracles that preprocesses a graph to build a data structure for answering approximate shortest path queries in constant time using landmark vertices and storing local and global distance information.
Dijkstra's algorithm finds the shortest paths between vertices in a graph with non-negative edge weights. It works by maintaining distances from the source vertex to all other vertices, initially setting all distances to infinity except the source which is 0. It then iteratively selects the unvisited vertex with the lowest distance, marks it as visited, and updates the distances to its neighbors if a shorter path is found through the selected vertex. This continues until all vertices are visited, at which point the distances will be the shortest paths from the source vertex.
This document discusses graph operations and representations. It provides examples of graph problems including path finding, connectedness problems, and spanning tree problems. It also discusses different representations of graphs like adjacency matrices and adjacency lists. It notes that 12 Java classes would be needed to fully represent directed/undirected weighted/unweighted graphs using adjacency matrices, linked lists, and array-based adjacency lists. It provides an example of an abstract Graph class that can be extended to implement different graph representations and types.
Prim's algorithm finds a minimum spanning tree of a graph using a greedy approach. It treats the nodes as a single tree and adds new nodes from the graph one by one, choosing the connection with the lowest weight at each step. This grows the tree until all nodes are included. Kruskal's algorithm also uses greedy approach but treats the graph as separate trees that merge as it adds edges from lowest to highest weight while ensuring no cycles are formed.
This document discusses minimum spanning trees and algorithms to find them. It begins by introducing the problem of laying cable in a new neighborhood along certain paths. It then defines spanning trees and minimum spanning trees. Two algorithms are described - Prim's and Kruskal's. Prim's grows the minimum spanning tree by adding one edge at a time, while Kruskal's grows a forest by adding edges until a single tree remains. The document applies Prim's algorithm to find the minimum spanning tree of a sample graph modeling the cable laying problem. It compares the time complexities of the two algorithms and discusses properties of minimum spanning trees.
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
This document compares the Bellman-Ford algorithm and Dijkstra's algorithm for finding shortest paths in graphs. Both algorithms can be used to find single-source shortest paths, but Bellman-Ford can handle graphs with negative edge weights while Dijkstra's algorithm cannot. Bellman-Ford has a worst-case time complexity of O(n^2) while Dijkstra's algorithm has a better worst-case time complexity of O(n^2). However, Dijkstra's algorithm is more efficient in practice for graphs with non-negative edge weights. The document provides pseudocode to describe the procedures of each algorithm.
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
Dijkstra's algorithm is used to find the shortest paths between nodes in a graph. It works by growing the shortest path tree one vertex at a time. It begins at the source node and examines all neighboring nodes, updating paths and tracking predecessors until the shortest path to every node has been determined. The Ford-Fulkerson method finds the maximum flow in a flow network by iteratively augmenting the flow along paths with available residual capacity until no more flow is possible. It works to push flow from the source to the sink along paths with available capacity until the network is saturated.
The document defines key terms related to graphs and networks. It provides examples of different types of graphs like trees, paths, and bipartite graphs. It also describes algorithms like Prim's and Kruskal's that are used to find the minimum spanning tree of a weighted network. Prim's algorithm works by greedily adding the lowest weight edge that connects an unconnected node. Kruskal's algorithm sorts all edges by weight and only adds an edge if it does not create a cycle. Both algorithms can be applied to distance matrices to find the minimum spanning tree.
The document discusses network optimization problems and the shortest path problem. It provides terminology for network components like nodes and arcs. It also classifies network problems and gives examples. The shortest path problem aims to find the path between two nodes with minimum total distance. The document illustrates applying Dijkstra's algorithm to solve a shortest path problem on a sample network from node 0 to node T, showing the iterations and calculations to determine the shortest distance. Students are assigned to use the algorithm to find the shortest distance from node 0 to node 6 on a given network.
Chennai Call Girls 7742996321 Escorts In Chennaimedonasharma
Chennai Call Girls 7742996321 Escorts In Chennai
More Related Content
Similar to APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV0V74UXL-lBqKxDd_Fn_Bl1IAafiengGrex1nn4IL86BQsAz9cSXxnxEtInomw8hp - dmoAOJIxs59hSNWeW1JIG0Y3wzys-xz98dtB0bCspm5CRUgDmhMDo1t1C7D3dNRyEM6glz.pptx
This document describes algorithms for finding minimum spanning trees in graphs. It discusses Prim's algorithm and Kruskal's algorithm, which are greedy algorithms used to find minimum spanning trees in undirected weighted graphs. It compares the time complexity of the two algorithms and notes that Prim's algorithm has lower time complexity and is simpler to implement. The document also outlines applications of minimum spanning trees and provides examples of applying Prim's and Kruskal's algorithms to find the minimum spanning tree of sample graphs.
This document discusses various algorithms for finding shortest paths in graphs, including Dijkstra's algorithm, breadth-first search (BFS), depth-first search (DFS), and Bellman-Ford algorithm. It provides pseudocode examples and explanations of how each algorithm works. It also covers properties of spanning trees, minimum spanning tree algorithms like Kruskal's and Prim's, and applications of spanning trees like network planning and routing protocols.
The document discusses greedy algorithms and their applications. It provides examples of problems that greedy algorithms can solve optimally, such as the change making problem and finding minimum spanning trees (MSTs). It also discusses problems where greedy algorithms provide approximations rather than optimal solutions, such as the traveling salesman problem. The document describes Prim's and Kruskal's algorithms for finding MSTs and Dijkstra's algorithm for solving single-source shortest path problems. It explains how these algorithms make locally optimal choices at each step in a greedy manner to build up global solutions.
This document discusses algorithms for solving the all-pairs shortest path problem in graphs. It defines the all-pairs shortest path problem as finding the shortest path between every pair of nodes in a graph. It then describes two main algorithms for solving this problem: Floyd-Warshall and Johnson's algorithm. Floyd-Warshall finds all-pairs shortest paths in O(n3) time using dynamic programming. Johnson's algorithm improves this to O(V2logV+VE) time by first transforming the graph to make edges positive, then running Dijkstra's algorithm from each node.
This document summarizes key concepts from a faculty development program on data structures, including graph applications, minimum spanning trees, shortest path algorithms, biconnected graphs, and Euler circuits. It provides examples and pseudocode for Prim's and Kruskal's minimum spanning tree algorithms and Dijkstra's shortest path algorithm. It also discusses identifying articulation points in a graph to determine if it is biconnected and conditions for the existence of Euler paths and circuits.
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHMABIRAMIS87
Kruskal's algorithm is used to find the minimum spanning tree (MST) of a connected, undirected graph. It works by sorting the edges by weight and building the MST by adding edges one by one if they do not form cycles. The MST has the minimum total weight among all spanning trees of the graph. Ford-Fulkerson algorithm finds the maximum flow in a flow network and uses augmenting paths to incrementally increase the flow until no more augmenting paths exist. Dijkstra's algorithm solves the single-source shortest path problem to find the shortest paths from a source vertex to all other vertices in a weighted graph.
Dijkstra's algorithm is used to find the shortest paths from a single source vertex to all other vertices in a graph. It works by maintaining two sets - a visited set containing vertices whose shortest paths are known, and an unvisited set of remaining vertices. It iteratively selects the vertex in the unvisited set with the shortest path, relaxes its edges to update path lengths, and moves it to the visited set until all vertices are processed. An example application of Dijkstra's algorithm on a sample graph is provided to find the shortest paths from vertex S to all other vertices.
This document provides an overview of minimal spanning trees, including basic terminology, applications, and algorithms. It defines a minimal spanning tree as a spanning tree (subgraph containing all vertices and no cycles) with the minimum sum of edge weights. Common applications include phone network design and electronic circuitry wiring. The document describes Prim's and Kruskal's algorithms for finding a minimal spanning tree in a graph. Prim's is a vertex-based greedy algorithm that grows the tree one vertex at a time. Kruskal's is an edge-based algorithm that considers edges in order of weight, adding them if they connect different trees.
The document discusses randomized graph algorithms and techniques for analyzing them. It describes a linear time algorithm for finding minimum spanning trees (MST) that samples edges and uses Boruvka's algorithm and edge filtering. It also discusses Karger's algorithm for approximating the global minimum cut in near-linear time using edge contractions. Finally, it presents an approach for 3-approximate distance oracles that preprocesses a graph to build a data structure for answering approximate shortest path queries in constant time using landmark vertices and storing local and global distance information.
Dijkstra's algorithm finds the shortest paths between vertices in a graph with non-negative edge weights. It works by maintaining distances from the source vertex to all other vertices, initially setting all distances to infinity except the source which is 0. It then iteratively selects the unvisited vertex with the lowest distance, marks it as visited, and updates the distances to its neighbors if a shorter path is found through the selected vertex. This continues until all vertices are visited, at which point the distances will be the shortest paths from the source vertex.
This document discusses graph operations and representations. It provides examples of graph problems including path finding, connectedness problems, and spanning tree problems. It also discusses different representations of graphs like adjacency matrices and adjacency lists. It notes that 12 Java classes would be needed to fully represent directed/undirected weighted/unweighted graphs using adjacency matrices, linked lists, and array-based adjacency lists. It provides an example of an abstract Graph class that can be extended to implement different graph representations and types.
Prim's algorithm finds a minimum spanning tree of a graph using a greedy approach. It treats the nodes as a single tree and adds new nodes from the graph one by one, choosing the connection with the lowest weight at each step. This grows the tree until all nodes are included. Kruskal's algorithm also uses greedy approach but treats the graph as separate trees that merge as it adds edges from lowest to highest weight while ensuring no cycles are formed.
This document discusses minimum spanning trees and algorithms to find them. It begins by introducing the problem of laying cable in a new neighborhood along certain paths. It then defines spanning trees and minimum spanning trees. Two algorithms are described - Prim's and Kruskal's. Prim's grows the minimum spanning tree by adding one edge at a time, while Kruskal's grows a forest by adding edges until a single tree remains. The document applies Prim's algorithm to find the minimum spanning tree of a sample graph modeling the cable laying problem. It compares the time complexities of the two algorithms and discusses properties of minimum spanning trees.
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
This document compares the Bellman-Ford algorithm and Dijkstra's algorithm for finding shortest paths in graphs. Both algorithms can be used to find single-source shortest paths, but Bellman-Ford can handle graphs with negative edge weights while Dijkstra's algorithm cannot. Bellman-Ford has a worst-case time complexity of O(n^2) while Dijkstra's algorithm has a better worst-case time complexity of O(n^2). However, Dijkstra's algorithm is more efficient in practice for graphs with non-negative edge weights. The document provides pseudocode to describe the procedures of each algorithm.
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
Dijkstra's algorithm is used to find the shortest paths between nodes in a graph. It works by growing the shortest path tree one vertex at a time. It begins at the source node and examines all neighboring nodes, updating paths and tracking predecessors until the shortest path to every node has been determined. The Ford-Fulkerson method finds the maximum flow in a flow network by iteratively augmenting the flow along paths with available residual capacity until no more flow is possible. It works to push flow from the source to the sink along paths with available capacity until the network is saturated.
The document defines key terms related to graphs and networks. It provides examples of different types of graphs like trees, paths, and bipartite graphs. It also describes algorithms like Prim's and Kruskal's that are used to find the minimum spanning tree of a weighted network. Prim's algorithm works by greedily adding the lowest weight edge that connects an unconnected node. Kruskal's algorithm sorts all edges by weight and only adds an edge if it does not create a cycle. Both algorithms can be applied to distance matrices to find the minimum spanning tree.
The document discusses network optimization problems and the shortest path problem. It provides terminology for network components like nodes and arcs. It also classifies network problems and gives examples. The shortest path problem aims to find the path between two nodes with minimum total distance. The document illustrates applying Dijkstra's algorithm to solve a shortest path problem on a sample network from node 0 to node T, showing the iterations and calculations to determine the shortest distance. Students are assigned to use the algorithm to find the shortest distance from node 0 to node 6 on a given network.
Similar to APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV0V74UXL-lBqKxDd_Fn_Bl1IAafiengGrex1nn4IL86BQsAz9cSXxnxEtInomw8hp - dmoAOJIxs59hSNWeW1JIG0Y3wzys-xz98dtB0bCspm5CRUgDmhMDo1t1C7D3dNRyEM6glz.pptx (20)
Biography and career of Gerry Falletta.pdfGerry Falletta
Gerry Falletta, hailing from Hamilton, Ontario, is notably the son of Italian immigrants in a locale revered for its strong Italian presence. As the first in his lineage to attain a university education and a law degree, he represents a beacon of achievement and pride for his family.
4. Topics for lab session
Session Topics
1 Implementation of Minimal Spanning Tree
2 Implementation of Shortest path Algorithm
5. Learning Resources
• Fundamentals of Data Structures, E. Horowitz and S. Sahni, 1977.
• Data Structures and Algorithms, Alfred V. Aho, John E. Hopperoft,
Jeffrey D. UIlman.
• Mark Allen Weiss, Data Structures and Algorithm Analysis in C, 2nd
ed., Pearson Education, 2015
• Reema Thareja, Data Structures Using C, 1st ed., Oxford Higher
Education, 2011
• Thomas H Cormen, Charles E Leiserson, Ronald L Revest, Clifford
Stein, Introduction to Algorithms 3rd ed., The MIT Press Cambridge,
2014
6. Graph - Spanning Tree
A tree that contains every vertex of a connected graph is called spanning tree. It
is an undirected tree consisting of only those edges that are necessary to
connect all the vertices of the original graph G.
CHARACTERISTICS
1. It is a subgraph of a graph G that contain all the vertex of graph G.
2. For any pair of vertices, there exists only one path between them.
3. It is a connected graph with no cycles.
4. A graph may have many spanning tree.
8. Graph - Minimum Spanning Tree
•Let G=(V,E,W) be any weighted graph. Then a spanning tree whose cost is
minimum is called minimum spanning tree. The cost of the spanning tree is
defined as the sum of the costs of the edges in that tree.
9. Minimum Spanning Tree
• A spanning tree is a subset of Graph G, which has all the vertices covered with
minimum possible number of edges.
• A complete undirected graph can have maximum nn-2 number of spanning trees,
where n is number of nodes.
10. General properties of spanning tree
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have same number of edges and vertices.
• Spanning tree does not have any cycle (loops)
• Removing one edge from spanning tree will make the graph disconnected i.e.
spanning tree is minimally connected.
• Adding one edge to a spanning tree will create a cycle or loop i.e. spanning tree
is maximally acyclic.
11. Mathematical properties of spanning tree
• Spanning tree has n-1 edges, where n is number of nodes (vertices)
• From a complete graph, by removing maximum
e-n+1 edges, we can construct a spanning tree.
• A complete graph can have maximum nn-2 number of spanning trees.
• So we can conclude here that spanning trees are subset of a connected Graph G
and disconnected Graphs do not have spanning tree.
12. Application of Spanning Tree
•Spanning tree is basically used to find minimum paths to connect all
nodes in a graph.
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
13. Minimum Spanning Tree (MST)
• In a weighted graph, a minimum spanning tree is a spanning tree that has
minimum weight that all other spanning trees of the same graph.
MST Algorithm
• Kruskal’s Algorithm
• Prim’s Algorithm
Both are greedy algorithms.
14. A cable company want to connect five villages to their network which currently
extends to the market town of Avonford. What is the minimum length of cable
needed?
Avonford Fingley
Brinleigh Cornwell
Donster
Edan
2
7
4
5
8 6
4
5
3
8
Example
15. We model the situation as a network, then the problem is to find the minimum
connector for the network
A F
B C
D
E
2
7
4
5
8 6
4
5
3
8
17. Select the shortest
edge in the network
ED 2
Kruskal’s Algorithm
A
F
B
C
D
2
7
4
5
8 6
4
5
3
8
E
18. Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
Kruskal’s Algorithm
A
F
B
C
D
2
7
4
5
8 6
4
5
3
8
E
19. Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4 (or AE 4)
Kruskal’s Algorithm
A
F
B
C
D
2
7
4
5
8 6
4
5
3
8
E
20. Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
Kruskal’s Algorithm
A
F
B
C
D
2
7
4
5
8 6
4
5
3
8
E
21. Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
BC 5 – forms a cycle
EF 5
Kruskal’s Algorithm
A
F
B
C
D
2
7
4
5
8 6
4
5
3
8
E
22. All vertices have been
connected.
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree: 18
Kruskal’s Algorithm
A
F
B
C
D
2
7
4
5
8 6
4
5
3
8
E
23. All vertices have been
connected.
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree: 18
Kruskal’s Algorithm
A
F
B
C
D
2
4
5
4
3
E
24. Kruskal Algorithm
Algorithm
• Remove all loops & Parallel Edges from the given graph
• Sort all the edges in non-decreasing order of their weight.
• Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If
cycle is not formed, include this edge. Else, discard it.
• Repeat step#2 until there are (V-1) edges in the spanning tree.
32. Prim’s Algorithm
• Create a set mstSet that keeps track of vertices already included in MST.
• Assign a key value to all vertices in the input graph. Initialize all key
values as INFINITE. Assign key value as 0 for the first vertex so that it is
picked first.
• While mstSet doesn’t include all vertices
• Pick a vertex u which is not there in mstSet and has minimum key value.
• Include u to mstSet.
• Update key value of all adjacent vertices of u. To update the key values, iterate
through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is less
than the previous key value of v, update the key value as weight of u-v
33. Minimum Spanning Tree Algorithms
2. Select the next shortest edge
which does not create a cycle
3. Repeat step 2 until all vertices
have been connected
Kruskal’s algorithm Prim’s algorithm
1. Select the shortest edge in a 1. Select any vertex
network
2. Select the shortest edge
connected to that vertex
3. Select the shortest edge
connected to any vertex already
connected
4. Repeat step 3 until all vertices
have been connected
39. Single-Source Shortest Path Problem
Single-Source Shortest Path Problem - The problem of finding
shortest paths from a source vertex v to all other vertices in the graph.
40. Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source shortest path
problem in graph theory.
Works on both directed and undirected graphs. However, all edges must
have nonnegative weights.
Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge
weights are nonnegative
Output: Lengths of shortest paths (or the shortest paths themselves) from a
given source vertex v∈V to all other vertices
41. 41
Approach
• The algorithm computes for each vertex u the distance to u from the start vertex
v, that is, the weight of a shortest path between v and u.
• the algorithm keeps track of the set of vertices for which the distance has been
computed, called the cloud C
• Every vertex has a label D associated with it. For any vertex u, D[u] stores an
approximation of the distance between v and u. The algorithm will update a D[u]
value when it finds a shorter path from v to u.
• When a vertex u is added to the cloud, its label D[u] is equal to the actual (final)
distance between the starting vertex v and vertex u.
42. 42
Dijkstra pseudocode
Dijkstra(v1, v2):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.
while List is not empty:
v := remove List vertex with minimum distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v, n)'s weight.
if dist is smaller than n's distance:
n's distance := dist.
n's previous := v.
reconstruct path from v2 back to v1,
following previous pointers.
43. A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 ∞
∞ ∞
∞
Pick vertex in List with minimum distance.
∞ ∞
Distance(source) = 0 Distance (all vertices
but source) = ∞
43
Example: Initialization
44. Example: Update neighbors' distance
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
∞ ∞
1
∞ ∞
Distance(B) = 2
Distance(D) = 1
44
45. Example: Remove vertex with minimum
distance
Pick vertex in List with minimum distance, i.e., D
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
∞ ∞
1
∞ ∞
45
46. Example: Update neighbors
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
9 5
Distance(C) = 1 + 2 = 3
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9
Distance(G) = 1 + 4 = 5
46
47. Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
4
2
2
8
5
1
0 2
3 3
1
Pick vertex in List with minimum distance (B) and update neighbors
6
Note : distance(D) not
updated since D is
already known and
distance(E) not updated
since it is larger than
previously computed
47
9 5
48. Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
9
48
5
No updating
Pick vertex List with minimum distance (E) and update neighbors
49. Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
8
49
5
Pick vertex List with minimum distance (C) and update neighbors
Distance(F) = 3 + 5 = 8
50. Example: Continued...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
6 5
Distance(F) = min (8, 5+1) = 6
Previous distance
Pick vertex List with minimum distance (G) and update neighbors
50
51. Example (end)
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
Pick vertex not in S with lowest cost (F) and update neighbors
6
51
5