-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFleury's Algorithm.cpp
More file actions
63 lines (62 loc) · 1.58 KB
/
Copy pathFleury's Algorithm.cpp
File metadata and controls
63 lines (62 loc) · 1.58 KB
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
#include<iostream>
#include<vector>
#define NODE 7
using namespace std;
int graph[NODE][NODE] = {{0, 1, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 1, 0},
{0, 1, 1, 0, 1, 1, 0},
{0, 1, 0, 1, 0, 1, 1},
{0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 0}};
int tempGraph[NODE][NODE];
int findStartVert(){
for(int i = 0; i<NODE; i++){
int deg = 0;
for(int j = 0; j<NODE; j++){
if(tempGraph[i][j])
deg++;
}
if(deg % 2 != 0)
return i;
}
return 0;
}
bool isBridge(int u, int v){
int deg = 0;
for(int i = 0; i<NODE; i++)
if(tempGraph[v][i])
deg++;
if(deg>1){
return false;
}
return true;
}
int edgeCount(){
int count = 0;
for(int i = 0; i<NODE; i++)
for(int j = i; j<NODE; j++)
if(tempGraph[i][j])
count++;
return count;
}
void fleuryAlgorithm(int start){
static int edge = edgeCount();
for(int v = 0; v<NODE; v++){
if(tempGraph[start][v]){
if(edge <= 1 || !isBridge(start, v)){
cout << start << "--" << v << " ";
tempGraph[start][v] = tempGraph[v][start] = 0;
edge--;
fleuryAlgorithm(v);
}
}
}
}
int main(){
for(int i = 0; i<NODE; i++)
for(int j = 0; j<NODE; j++)
tempGraph[i][j] = graph[i][j];
cout << "Euler Path Or Circuit: ";
fleuryAlgorithm(findStartVert());
}