0% found this document useful (0 votes)
69 views3 pages

Prims Algorithms

The document summarizes an informal definition of a graph and minimum spanning tree. It then describes Prim's algorithm for finding a minimum spanning tree and walks through an example. Specifically, it begins with only one node in the tree, then iteratively adds the closest node outside the tree and the minimum weight edge connecting them, until there are n-1 edges in the tree.

Uploaded by

nofeelingrahul
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
69 views3 pages

Prims Algorithms

The document summarizes an informal definition of a graph and minimum spanning tree. It then describes Prim's algorithm for finding a minimum spanning tree and walks through an example. Specifically, it begins with only one node in the tree, then iteratively adds the closest node outside the tree and the minimum weight edge connecting them, until there are n-1 edges in the tree.

Uploaded by

nofeelingrahul
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

n this article I give an informal definition of a graph and of the minimum spanning tree.

Afterwards I describe Prims algorithm and then follow its execution on an example. Finally, the code in C is provided. Graphs Wikipedia gives one of the common definitions of a graph: In computer science, a graph is a kind of data structure, specifically an abstract data type (ADT), that consists of a set of nodes and a set of edges that establish relationships (connections) between the nodes. The graph ADT follows directly from the graph concept from mathematics. Informally, G=(V,E) consists of vertices, the elements of V, which are connected by edges, the elements of E. Formally, a graph, G, is defined as an ordered pair, G=(V,E), where V is a finite set and E is a set consisting of two element subsets of V. This is a graph:

Its a set of nodes (A, B, C, D and E) and the edges (lines) that interconnect them. An important thing to note about this graph is that the edges are bidirectional, i.e. if A is connected to B, then B is connected to A. This makes it an undirected graph. A common extension is to attribute weights to the edges. This is what Ive done with the previous graph:

Minimum spanning trees Basically a minimum spanning tree is a subset of the edges of the graph, so that theres a path form any node to any other node and that the sum of the weights of the edges is minimum.

Heres the minimum spanning tree of the example:

Look at the above image closely. It contains all of the initial nodes and some of the initial edges. Actually it contains exactly n 1 edges, where n is the number of nodes. Its called a tree because there are no cycles. You can think of the graph as a map, with the nodes being cities, the edges passable terrain, and the weights the distance between the cities. Its worth mentioning that a graph can have several minimum spanning trees. Think of the above example, but replace all the weight with 1. The resulting graph will have 6 minimum spanning trees. Given a graph, find one of its minimum spanning trees. Prims Algorithm One of the classic algorithms for this problem is that found by Robert C. Prim. Its a greedy style algorithm and its guaranteed to produce a correct result. In the following discussion, let the distance from each node not in the tree to the tree be the edge of minimal weight between that node and some node in the tree. If there is no such edge, assume the distance is infinity (this shouldnt happen). The algorithm (greedily) builds the minimal spanning tree by iteratively adding nodes into a working tree: 1. Start with a tree which contains only one node. 2. Identify a node (outside the tree) which is closest to the tree and add the minimum weight edge from that node to some node in the tree and incorporate the additional node as a part of the tree. 3. If there are less then n 1 edges in the tree, go to 2 For the example graph, heres how it would run:

Start with only node A in the tree.

Find the closest node to the tree, and add it.

Repeat until there are n 1 edges in the tree.

You might also like