-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
108 lines (95 loc) · 1.94 KB
/
Graph.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
103
104
105
106
107
108
#include<iostream>
using namespace std;
#define num 5
struct vertex{
int vno;
vertex*Connected[num];
vertex*next;
vertex*prev;
};
vertex*head = NULL;
vertex*tail = NULL;
void CreateVertex(){
for (int i = 0; i < num; i++){
vertex*new_vertex;
new_vertex = new vertex();
if (head == NULL){
new_vertex->vno = i + 1;
new_vertex->next = NULL;
new_vertex->prev = NULL;
head = new_vertex;
tail = new_vertex;
}
else{
new_vertex->vno = i + 1;
new_vertex->next = NULL;
tail->next = new_vertex;
new_vertex->prev = tail;
tail = new_vertex;
}
for (int i = 0; i < num; i++){
new_vertex->Connected[i] = NULL;
}
}
}
void DisplayVertex(){
vertex*toDisplay=head;
while (toDisplay != NULL){
cout << toDisplay->vno << endl;
cout << "Connected Vertex's : ";
for (int i = 0; i < num; i++){
if (toDisplay->Connected[i] == NULL){
//cout << "NULL ";
}
else{
cout << toDisplay->Connected[i]->vno <<" ";
}
}
cout << endl;
toDisplay = toDisplay->next;
}
}
void ConnectVertex(int VTC,int VWC,int Count){
vertex*edges = head;
vertex*toConnect = NULL;
vertex*withConnect = NULL;
while (edges != NULL){
if (edges->vno == VTC)
{
toConnect = edges;
break;
}
edges = edges->next;
}
edges = head;
while (edges != NULL){
if (edges->vno == VWC)
{
withConnect = edges;
break;
}
edges = edges->next;
}
for (int i = 0; i < num; i++){
if (withConnect->Connected[i] == NULL){
withConnect->Connected[i] = toConnect;
break;
}
}
if (Count < 2){
ConnectVertex(VWC, VTC, Count + 1);
}
}
int main(){
CreateVertex();
//Graph Representation
ConnectVertex(2, 1, 1);
ConnectVertex(5, 1, 1);
ConnectVertex(5, 2, 1);
ConnectVertex(4, 2, 1);
ConnectVertex(3, 2, 1);
ConnectVertex(4, 3, 1);
ConnectVertex(5, 4, 1);
DisplayVertex();
system("pause");
}