Giter Site home page Giter Site logo

sleekpanther / kruskals-algorithm-minimum-spanning-tree-mst Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 8.0 727 KB

Kruskal's Algorithm (greedy) to find a Minimum Spanning Tree on a graph

Java 100.00%
graph graphs kruskal-algorithm mst weight disjoint-sets java edges minimum-spanning-trees tree

kruskals-algorithm-minimum-spanning-tree-mst's Introduction

Kruskal's Algorithm Minimum Spanning Tree (Graph MST)

Java Implementation of Kruskal's Algorithm using disjoing sets
Kruskal's algorithm: Start with T = โˆ…. Consider edges in ascending order of weight. Insert edge e into T unless doing so would create a cycle.

  • Works on UN-directed graphs
  • Algorithm still works on edges with identical weight
    Edges are sorted by weight first. Depending on the order they are entered into the edge list in the constructor, you may get different Minimum Spanning Trees (but all still optimal solutions)

PseudoCode

kruskal-pseudocode

Detailed Implementation

kruskal-detailed-implementation

Code Notes

Current code runs on this sample graph graph It produces this Minimum Spanning Tree mst

  • Requires distinct nodes named with consecutive integers. If they really do have the same "name", convert them to integer ID's to use this algorithm
    Example graph has 8 nodes numbered 1-8 (array ignores the 0th index)
  • Graph is created as an Edge List
  • Make sure nodeCount is accurate. There's no error checking between nodeCount & the actual edge list
  • DisjointSet nodeSet = new DisjointSet(nodeCount+1); skips the 0th index by creating a Disjoint Set 1 larger than the number of vertices
  • outputMessage is a string that records the steps the algorithm takes. It's printed to the screen & to a file once complete
  • My implementation has early termination (A Spanning Tree of a graph has N-1 edges so the algorithm stops whenn it has added N-1 Edges) Hence && mstEdges.size()<(nodeCount-1) in my loop
  • The Edge class simply packages an edge's weight & 2 vertices together as 1 object

Sources

kruskals-algorithm-minimum-spanning-tree-mst's People

Contributors

sleekpanther avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.