Git Product home page Git Product logo

prim-mst's Introduction

Prim's Algorithm

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. Also called Jarník's algorithm, it begins by choosing an arbitrary vertex of the graph, and constructs a tree one edge at a time, growing until the tree spans the entire vertex set. In each iteration, it greedily adds the cheapest edge that extends the reach of the tree so far.

The straightforward implementation of the Prim's algorithm is fast enough to process medium-size graphs (with thousands of vertices and edges) in a reasonable amount of time, but not big graphs (with millions of vertices and edges). In order to achieve a near-linear-time solution to the problem, we don't need a better algorithm, just a better implementation of Prim's algorithm. The straightforward implementation performs repeated minumum computations, therefore the use of a heap data structure enables a blazingly fast, near-linear-time implementation.

The problem

The input file describes an undirected graph with integer edge costs. You should not assume that edge costs are always positive, nor that they are distinct.

The task is to run Prim's minimum spanning tree algorithm on this graph. You should report the overall cost of the minumum spanning tree -- an integer, which may or may not be negative.

This graph is small enough that the straightforward O(mn) time implementation of Prim's algorithm should work fine. For those seeking an additional challenge, try implementing a heap-based version. The simpler approach, which should already give you a healthy speed-up, is to maintain relevant edges in a heap (with keys = edge costs). The superior approach stores the unprocessed vertices in the heap, as described in lecture. Note this requires a heap that supports deletions, and you'll probably need to maintain some kind of mapping between vertices and their positions in the heap.

References

The information presented here and the problem were taken from the book Algorithms Illuminated, by Tim Roughgarden.

prim-mst's People

Contributors

msgaspar avatar

Stargazers

Roman avatar

Watchers

 avatar

Forkers

laiba1510

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.