forked from Turupawn/FindCycleInGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (43 loc) · 751 Bytes
/
main.cpp
File metadata and controls
48 lines (43 loc) · 751 Bytes
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
#include "Test.h"
#include <iostream>
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 hasCycle(int** graph, int size)
{
//graph[fromNode][toNode] = cost
int currentNode = -1;
int cost = -1;
bool visitedNodes[size];
initVisitedArray(visitedNodes, size);
for(int i = 0; i < size; ++i)
{
visitedNodes[i] = true;
for(int k = 0; k < size; ++k)
{
cost = graph[i][k];
if(cost != -1)
{
if(nodeHasBeenVisited(visitedNodes, k))
{
return true;
}
}
}
}
return false;
}
int main ()
{
test();
return 0;
}