forked from Turupawn/KruskalMST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
102 lines (89 loc) · 1.92 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Test.h"
#include <iostream>
#include "Edge.h"
using namespace std;
bool nodeHasBeenVisited(bool* visitedNodes, int currentNodeIndex)
{
return visitedNodes[currentNodeIndex];
}
void initVisitedArray(bool* visited, int size)
{
for(int i = 0; i < size; ++i)
{
visited[i] = false;
}
}
bool checkCycleThroughNode(int** graph, int size, bool* visited, int father, int node)
{
bool value = false;
if (node < size)
{
visited[node] = true;
cout<<"Checking father: "<<father<<" and son: "<<node<<endl;
for (int i = 0; i < size; ++i)
{
if (i != node && i != father)
{
//cout<<"Checking father: "<<father<<" with: "<<i<<endl;
if (graph[node][i] != -1)
{
if (visited[i])
{
return true;
}else{
value |= checkCycleThroughNode(graph, size, visited, node, i);
}
}
}
}
}
return value;
}
bool hasCycle(int** graph, int size, Edge* edge)
{
bool visitedNodes[size];
initVisitedArray(visitedNodes, size);
graph[edge->source][edge->destination] = edge->weight;
graph[edge->destination][edge->source] = edge->weight;
bool hasCycleWithNode = checkCycleThroughNode(graph, size, visitedNodes, 0, 0);
if(hasCycleWithNode)
{
cout<<"Had cycle"<<endl;
graph[edge->source][edge->destination] = -1;
graph[edge->destination][edge->source] = -1;
}
return hasCycleWithNode;
}
int** initGraph(int size)
{
int **answer = new int* [size];
for(int i=0;i<size;i++)
{
answer[i]=new int[size];
for(int j=0;j<size;j++)
{
answer[i][j]=-1;
}
}
return answer;
}
int** getKruskalMST(int** graph, int size, vector<Edge*> edges)
{
int** kruskalGraph = initGraph(size);
int addedEdges = 0;
unsigned int i = 0;
cout<<"Running"<<endl;
while(i < edges.size())
{
if(!hasCycle(kruskalGraph, size, edges[i++]))
{
}
//++addedEdges;
}
return kruskalGraph;
}
int main ()
{
test();
return 0;
}