Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

NotificationsYou must be signed in to change notification settings

SleekPanther/kruskals-algorithm-minimum-spanning-tree-mst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Implementation of Kruskal's Algorithm usingdisjoing sets
Kruskal's algorithm: Start withT = ∅. Consider edges in ascending order of weight. Insert edgee intoT unless doing so would create a cycle.

  • Works onUN-directed graphs
  • Algorithm still works on edges with identical weight
    Edges are sorted by weight first. Depending on the order they are entered into theedge list in theconstructor, 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 graphgraphIt produces this Minimum Spanning Treemst

  • Requiresdistinct nodes named with consecutive integers. If they reallydo 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 surenodeCount is accurate. There's no error checking betweennodeCount & the actual edge list
  • DisjointSet nodeSet = new DisjointSet(nodeCount+1); skips the 0th index by creating a Disjoint Set1 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 hasearly termination (A Spanning Tree of a graph hasN-1 edges so the algorithm stops whenn it has addedN-1 Edges)Hence&& mstEdges.size()<(nodeCount-1) in my loop
  • TheEdge class simply packages an edge's weight & 2 vertices together as 1 object

Sources


[8]ページ先頭

©2009-2025 Movatter.jp