|
| 1 | +package graph |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | +) |
| 7 | + |
| 8 | +//======================================== Graph Structure Implementation starts========================================// |
| 9 | + |
| 10 | +//Edge => an edge struct which represent a edge from vertex A to vertex B having X Weight |
| 11 | +type Edge struct { |
| 12 | + From int |
| 13 | + To int |
| 14 | + Weight int |
| 15 | +} |
| 16 | + |
| 17 | +//Graph => graph data structure |
| 18 | +type Graph struct { |
| 19 | + vertices int |
| 20 | + edges [][]Edge |
| 21 | +} |
| 22 | + |
| 23 | +//Initialize => initializes the graph |
| 24 | +func (g *Graph) Initialize(vertices int) { |
| 25 | + |
| 26 | + //set the vertices we have |
| 27 | + g.vertices = vertices |
| 28 | + |
| 29 | + // initialize the vertices |
| 30 | + if g.edges == nil { |
| 31 | + g.edges = make([][]Edge, vertices) |
| 32 | + } |
| 33 | + |
| 34 | + //initialize each vertex as an empty array |
| 35 | + for i := 0; i < vertices; i++ { |
| 36 | + g.edges[i] = []Edge{} |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +//AddEdge => weighted edge add |
| 41 | +func (g *Graph) AddEdge(u int, v int, w int) { |
| 42 | + currEdge := Edge{u, v, w} |
| 43 | + |
| 44 | + //for undirected graph both should have the entry |
| 45 | + currEdgeU := Edge{v, u, w} |
| 46 | + |
| 47 | + //set the v is reachabe from u |
| 48 | + g.edges[u] = append(g.edges[u], currEdge) |
| 49 | + g.edges[v] = append(g.edges[v], currEdgeU) |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +//Print the graph to see the Adjacency List |
| 54 | +func (g *Graph) Print() { |
| 55 | + |
| 56 | + for i := range g.edges { |
| 57 | + fmt.Printf("%v\n", g.edges[i]) |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +//========================================= Graph Implementation Ends Here =======================================// |
| 63 | + |
| 64 | +/* |
| 65 | +# A utility function to find set of an element i |
| 66 | +
|
| 67 | +
|
| 68 | + # A function that does union of two sets of x and y |
| 69 | + # (uses union by rank) |
| 70 | + def union(self, parent, rank, x, y): |
| 71 | + xroot = self.find(parent, x) |
| 72 | + yroot = self.find(parent, y) |
| 73 | +
|
| 74 | + # Attach smaller rank tree under root of |
| 75 | + # high rank tree (Union by Rank) |
| 76 | + if rank[xroot] < rank[yroot]: |
| 77 | + parent[xroot] = yroot |
| 78 | + elif rank[xroot] > rank[yroot]: |
| 79 | + parent[yroot] = xroot |
| 80 | +
|
| 81 | + # If ranks are same, then make one as root |
| 82 | + # and increment its rank by one |
| 83 | + else : |
| 84 | + parent[yroot] = xroot |
| 85 | + rank[xroot] += 1 |
| 86 | +*/ |
| 87 | + |
| 88 | +//traverses the parents array and fetches the representative parent of the vertex x |
| 89 | +func findParent(parent []int, i int) int { |
| 90 | + if parent[i] == i { |
| 91 | + return i |
| 92 | + } |
| 93 | + return findParent(parent, parent[i]) |
| 94 | +} |
| 95 | + |
| 96 | +//Merges two vertices and sets the parent by rank |
| 97 | +func union(parent []int, rank []int, x int, y int) { |
| 98 | + xroot := findParent(parent, x) |
| 99 | + yroot := findParent(parent, y) |
| 100 | + |
| 101 | + if rank[x] > rank[y] { |
| 102 | + parent[yroot] = xroot |
| 103 | + } else if rank[x] < rank[y] { |
| 104 | + parent[xroot] = yroot |
| 105 | + } else { |
| 106 | + parent[yroot] = xroot |
| 107 | + rank[xroot] = 1 |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Kruskals algorithm |
| 112 | +func Kruskals(g Graph) { |
| 113 | + |
| 114 | + //get all the edges in an array and then sort it |
| 115 | + sortedEdges := []Edge{} |
| 116 | + MST := []Edge{} |
| 117 | + |
| 118 | + //TODO : Use a min Heap to optimize the following |
| 119 | + for i := range g.edges { |
| 120 | + for edge := range g.edges[i] { |
| 121 | + sortedEdges = append(sortedEdges, g.edges[i][edge]) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + sort.Slice(sortedEdges, func(i, j int) bool { |
| 126 | + return sortedEdges[i].Weight < sortedEdges[j].Weight |
| 127 | + }) |
| 128 | + |
| 129 | + //initialize parent and the rank |
| 130 | + parent := make([]int, g.vertices) |
| 131 | + rank := make([]int, g.vertices) |
| 132 | + |
| 133 | + for i := 0; i < g.vertices; i++ { |
| 134 | + parent[i] = i |
| 135 | + rank[i] = 0 |
| 136 | + } |
| 137 | + |
| 138 | + i := 0 |
| 139 | + e := 0 |
| 140 | + |
| 141 | + //we need to select V-1 edges for all the edges to connect |
| 142 | + for e < g.vertices-1 { |
| 143 | + |
| 144 | + //select the minumum edge not choosen yet |
| 145 | + currEdge := sortedEdges[i] |
| 146 | + i++ |
| 147 | + |
| 148 | + //find the parent of the vertices of current edge |
| 149 | + x := findParent(parent, currEdge.From) |
| 150 | + y := findParent(parent, currEdge.To) |
| 151 | + |
| 152 | + //if parent of x is not equal to parent of y means both have different sets and thus wont form a cycle when merged |
| 153 | + //so we select this edge and increase the edges count |
| 154 | + if x != y { |
| 155 | + e++ |
| 156 | + //add it to mst |
| 157 | + MST = append(MST, currEdge) |
| 158 | + //since the vertices are now connected union both of them for future use. |
| 159 | + union(parent, rank, x, y) |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + fmt.Println("PRINTING THE MST") |
| 164 | + fmt.Println(MST) |
| 165 | + |
| 166 | +} |
0 commit comments