Skip to content

Commit f78e0ea

Browse files
committed
feature: mst kruskal initial code
1 parent 3bacc51 commit f78e0ea

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include<map>
4+
#include<vector>
5+
using namespace std;
6+
7+
namespace MinimumSpanningTreeKruskalAlgorithm
8+
{
9+
class Node
10+
{
11+
public:
12+
int data;
13+
Node* parent;
14+
int rank;
15+
Node(int data);
16+
};
17+
18+
class Edge
19+
{
20+
public:
21+
Node* nodeU;
22+
Node* nodeV;
23+
int weight;
24+
Edge(Node* nodeU, Node* nodeV, int weight);
25+
bool CompareEdges(Edge* edgeX, Edge* edgeY);
26+
};
27+
28+
class Graph
29+
{
30+
private:
31+
map<Node*, vector<Node*>> _adjlist;
32+
map<int, Node*> _nodeMap;
33+
Node* MakeOrFindNode(int data);
34+
void MakeSet(Node* node);
35+
void Union(Node* nodeU, Node* nodeV);
36+
void Link(Node* nodeU, Node* nodeV);
37+
Node* FindSet(Node* node);
38+
39+
public:
40+
void PushUndirectedEdge(int valueU, int valueV);
41+
void FindMinimumSpanningTreeKruskalAlgorithm();
42+
};
43+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "../Headers/0003_Graph/0007_MinimumSpanningTreeKruskalAlgorithm.h"
2+
#include<climits>
3+
using namespace std;
4+
5+
namespace MinimumSpanningTreeKruskalAlgorithm
6+
{
7+
Node::Node(int data)
8+
{
9+
this->data = data;
10+
this->parent = nullptr;
11+
this->rank = INT_MAX;
12+
}
13+
14+
Edge::Edge(Node* nodeU, Node* nodeV, int weight)
15+
{
16+
this->nodeU = nodeU;
17+
this->nodeV = nodeV;
18+
this->weight = weight;
19+
}
20+
21+
bool Edge::CompareEdges(Edge* edgeX, Edge* edgeY)
22+
{
23+
return (edgeX->weight < edgeY->weight);
24+
}
25+
26+
// Graph Private Member Methods
27+
Node* Graph::MakeOrFindNode(int data)
28+
{
29+
Node* node = nullptr;
30+
if (this->_nodeMap.find(data) == this->_nodeMap.end())
31+
{
32+
node = new Node(data);
33+
this->_nodeMap[data] = node;
34+
}
35+
else
36+
{
37+
node = this->_nodeMap[data];
38+
}
39+
return node;
40+
}
41+
42+
void Graph::MakeSet(Node* node)
43+
{
44+
node->parent = node;
45+
node->rank = 0;
46+
}
47+
48+
void Graph::Union(Node* nodeU, Node* nodeV)
49+
{
50+
this->Link(this->FindSet(nodeU), this->FindSet(nodeV));
51+
}
52+
53+
void Graph::Link(Node* nodeU, Node* nodeV)
54+
{
55+
if (nodeV->rank > nodeU->rank)
56+
{
57+
nodeU->parent = nodeV;
58+
}
59+
else
60+
{
61+
nodeV->parent = nodeU;
62+
if (nodeU->rank == nodeV->rank)
63+
{
64+
nodeU->rank += 1;
65+
}
66+
}
67+
}
68+
69+
Node* Graph::FindSet(Node* node)
70+
{
71+
if (node != node->parent)
72+
{
73+
node->parent = this->FindSet(node->parent);
74+
}
75+
return node->parent;
76+
}
77+
78+
// Graph Public Member Methods
79+
void Graph::PushUndirectedEdge(int dataU, int dataV)
80+
{
81+
Node* nodeU = this->MakeOrFindNode(dataU);
82+
Node* nodeV = this->MakeOrFindNode(dataV);
83+
84+
this->_adjlist[nodeU].push_back(nodeV);
85+
this->_adjlist[nodeV].push_back(nodeU);
86+
}
87+
}

SourceCodes/0003_Graph/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ set(0003GRAPH_SOURCES
66
0004_StronglyConnectedComponents.cc
77
0005_HamiltonianPathAndCycle.cc
88
0006_EulerianPathAndCircuit.cc
9+
0007_MinimumSpanningTreeKruskalAlgorithm.cc
10+
911
)
1012

1113
# Create a library target

Tests/0003_Graph/0007_MinimumSpanningTreeKruskalAlgorithmTest.cc

Whitespace-only changes.

Tests/0003_Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_executable(
1818
0004_StronglyConnectedComponentsTest.cc
1919
0005_HamiltonianPathAndCycleTest.cc
2020
0006_EulerianPathAndCircuitTest.cc
21+
0007_MinimumSpanningTreeKruskalAlgorithmTest.cc
2122
)
2223

2324
target_link_libraries(

0 commit comments

Comments
 (0)