concept minimum span tree in category algorithms

appears as: minimum spning tree, minimum spanning tree, A minimum spning tree, minimum spning tree, minimum spning trees, The minimum spning tree
Classic Computer Science Problems in Java

This is an excerpt from Manning's book Classic Computer Science Problems in Java.

A connected graph is a graph that has some way of getting from any vertex to any other vertex. (All of the graphs we are looking at in this chapter are connected.) A spanning tree is a tree that connects every vertex in a graph. A minimum spanning tree is a tree that connects every vertex in a weighted graph with the minimum total weight (compared to other spanning trees). For every connected weighted graph, it is possible to efficiently find its minimum spanning tree.

Whew—that was a lot of terminology! The point is that finding a minimum spanning tree is the same as finding a way to connect every vertex in a weighted graph with the minimum weight. This is an important and practical problem for anyone designing a network (transportation network, computer network, and so on): how can every node in the network be connected for the minimum cost? That cost may be in terms of wire, track, road, or anything else. For instance, for a telephone network, another way of posing the problem is “What is the minimum length of cable one needs to connect every phone?”

Thanks to the pretty-printing printWeightedPath() method, the minimum spanning tree is easy to read.

Seattle 678.0> San Francisco
San Francisco 348.0> Los Angeles
Los Angeles 50.0> Riverside
Riverside 307.0> Phoenix
Phoenix 887.0> Dallas
Dallas 225.0> Houston
Houston 702.0> Atlanta
Atlanta 543.0> Washington
Washington 123.0> Philadelphia
Philadelphia 81.0> New York
New York 190.0> Boston
Washington 396.0> Detroit
Detroit 238.0> Chicago
Atlanta 604.0> Miami
Total Weight: 5372.0

In other words, this is the cumulatively shortest collection of edges that connects all of the MSAs in the weighted graph. The minimum length of track needed to connect all of them is 5,372 miles. Figure 4.7 illustrates the minimum spanning tree.

Figure 4.7 The highlighted edges represent a minimum spanning tree that connects all 15 MSAs.
Classic Computer Science Problems in Python

This is an excerpt from Manning's book Classic Computer Science Problems in Python.

A connected graph is a graph that has some way of getting from any vertex to any other vertex. (All of the graphs we are looking at in this chapter are connected.) A spanning tree is a tree that connects every vertex in a graph. A minimum spanning tree is a tree that connects every vertex in a weighted graph with the minimum total weight (compared to other spanning trees). For every weighted graph, it is possible to efficiently find its minimum spanning tree.

Whew—that was a lot of terminology! The point is that finding a minimum spanning tree is the same as finding a way to connect every vertex in a weighted graph with the minimum weight. This is an important and practical problem for anyone designing a network (transportation network, computer network, and so on): how can every node in the network be connected for the minimum cost? That cost may be in terms of wire, track, road, or anything else. For instance, for a telephone network, another way of posing the problem is “What is the minimum length of cable one needs to connect every phone?”

The algorithm returns an optional WeightedPath representing the minimum spanning tree. It does not matter where the algorithm starts (assuming the graph is connected and undirected), so the default is set to vertex index 0. If it so happens that the start is invalid, mst() returns None.

result: WeightedPath = [] # holds the final MST
pq: PriorityQueue[WeightedEdge] = PriorityQueue()
visited: [bool] = [False] * wg.vertex_count # where we've been


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":19},{\"line\":2,\"ch\":24}]]"}
!@%STYLE%@!

result will ultimately hold the weighted path containing the minimum spanning tree. This is where we will add WeightedEdges, as the lowest-weight edge is popped off and takes us to a new part of the graph. Jarník’s algorithm is considered a greedy algorithm because it always selects the lowest-weight edge. pq is where newly discovered edges are stored and the next-lowest-weight edge is popped. visited keeps track of vertex indices that we have already been to. This could also have been accomplished with a Set, similar to explored in bfs().

Thanks to the pretty-printing printWeightedPath() method, the minimum spanning tree is easy to read.

Seattle 678> San Francisco
San Francisco 348> Los Angeles
Los Angeles 50> Riverside
Riverside 307> Phoenix
Phoenix 887> Dallas
Dallas 225> Houston
Houston 702> Atlanta
Atlanta 543> Washington
Washington 123> Philadelphia
Philadelphia 81> New York
New York 190> Boston
Washington 396> Detroit
Detroit 238> Chicago
Atlanta 604> Miami
Total Weight: 5372

In other words, this is the cumulatively shortest collection of edges that connects all of the MSAs in the weighted graph. The minimum length of track needed to connect all of them is 5,372 miles. Figure 4.7 illustrates the minimum spanning tree.

Figure 4.7. The highlighted edges represent a minimum spanning tree that connects all 15 MSAs.
Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

This is an excerpt from Manning's book Classic Computer Science Problems in Swift: Essential techniques for practicing programmers.

A connected graph is a graph that has some way of getting from any vertex to any other vertex (all of the graphs we are looking at in this chapter are connected). A spanning tree is a tree that connects every vertex in a graph. A minimum spanning tree is a tree that connects every vertex in a weighted graph with the minimum total weight (compared to other spanning trees). For every weighted graph, it is possible to efficiently find its minimum spanning tree.

Whew, that was a lot of terminology! The point is that finding a minimum spanning tree is the same as finding a way to connect every vertex in a weighted graph with the minimum weight. This is an important and practical problem for anyone designing a network (transportation network, computer network, and so on)—how can every node in the network be connected for the minimum cost? That cost may be in terms of wire, track, road, or anything else. For instance, for a telephone network, another way of posing the problem is, “what is the minimum length of cable one needs to connect every phone?”

/// Extensions to WeightedGraph for building a Minimum-Spanning Tree (MST)
public extension WeightedGraph {
    typealias WeightedPath = [WeightedEdge<W>]

    /// Find the minimum spanning tree in a weighted graph. This is the set
         of edges
    /// that touches every vertex in the graph and is of minimal combined
         weight. This function
    /// uses Jarnik's algorithm (aka Prim's algorithm) and so assumes the
         graph has
    /// undirected edges. For a graph with directed edges, the result may
         be incorrect. Also,
    /// if the graph is not fully connected, the tree will only span the
         connected component from which
    /// the starting vertex belongs.
    ///
    /// - parameter start: The index of the vertex to start creating
         the MST from.
    /// - returns: An array of WeightedEdges containing the minimum
         spanning tree, or nil if the starting vertex is invalid. If
         there are is only one vertex connected to the starting vertex,
         an empty list is returned.
    public func mst(start: Int = 0) -> WeightedPath? {
        if start > (vertexCount - 1) || start < 0 { return nil }
        var result: [WeightedEdge<W>] = [WeightedEdge<W>]() // the final
         MST goes in here
        var pq: PriorityQueue<WeightedEdge<W>> =
         PriorityQueue<WeightedEdge<W>>(ascending: true) // minPQ
        var visited: [Bool] = Array<Bool>(repeating: false, count:
         vertexCount) // already been to these

        func visit(_ index: Int) {
            visited[index] = true // mark as visited
            for edge in edgesForIndex(index) { // add all edges coming from
             here to pq
                if !visited[edge.v] { pq.push(edge) }
            }
        }

        visit(start) // the first vertex is where everything begins

        while let edge = pq.pop() { // keep going as long as there are
         edges to process
            if visited[edge.v] { continue } // if we've been both places,
             ignore
            result.append(edge) // otherwise this is the current smallest
             so add it to the result set
            visit(edge.v) // visit where this connects
        }

        return result
    }

    /// Pretty-print an edge list returned from an MST
    /// - parameter edges The edge array representing the MST
    public func printWeightedPath(_ weightedPath: WeightedPath) {
        for edge in weightedPath {
            print("\(vertexAtIndex(edge.u)) \(edge.weight)>
             \(vertexAtIndex(edge.v))")
        }
        if let tw = totalWeight(weightedPath) {
            print("Total Weight: \(tw)")
        }
    }
}

Thanks to the pretty-printing printWeightedPath() method, the minimum spanning tree is easy to read.

Seattle 678> San Francisco
San Francisco 348> Los Angeles
Los Angeles 50> Riverside
Riverside 307> Phoenix
Phoenix 887> Dallas
Dallas 225> Houston
Houston 702> Atlanta
Atlanta 543> Washington
Washington 123> Philadelphia
Philadelphia 81> New York
New York 190> Boston
Washington 396> Detroit
Detroit 238> Chicago
Atlanta 604> Miami
Total Weight: 5372

In other words, this is the cumulatively shortest collection of edges that connects all of the MSAs in the weighted graph. The minimum length of track needed to connect all of them is 5372 miles. Figure 4.7 illustrates the minimum spanning tree.

Figure 4.7. The highlighted edges represent a minimum spanning tree that connects all 15 MSAs.