-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp2Graph.h
92 lines (82 loc) · 1.79 KB
/
temp2Graph.h
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
#define max 1001
#include <cstdlib>
#include <iostream>
using namespace std;
struct edgelist{
int value;
int weight;
struct edgelist *next;
};
class Graph{
public:
edgelist *edges[max];
int InDegree[max];
int OutDegree[max];
int vertex_count = 0;
int edge_count = 0;
int directed = 0;
void showGraph();
void addVertex(int n);
void addEdge(int x,int y);
};
//n is number of new vertices
void Graph::addVertex(int n)
{
for(int i = vertex_count + 1; i <= vertex_count + n; i++)
{
edges[i] = NULL;
InDegree[i] = 0;
OutDegree[i] = 0;
}
vertex_count += n;
};
void Graph::addEdge(int x,int y)
{
struct edgelist *temp1 = (struct edgelist *) malloc(sizeof(struct edgelist *));
temp1->value = y;
InDegree[y]++;
OutDegree[x]++;
if(edges[x] == NULL)//IF edgelist of vertex x is empty
{
edges[x] = temp1;
}
else
{
temp1->next = edges[x];
edges[x] = temp1;
}
edge_count++;
if(directed == 0)
{
struct edgelist *temp2 = (struct edgelist *) malloc(sizeof(struct edgelist *));
temp2->value = x;
InDegree[x]++;
OutDegree[y]++;
if(edges[y] == NULL)
{
edges[y] = temp2;
}
else
{
temp2->next = edges[y];
edges[y] = temp2;
}
edge_count++;
}
};
void Graph::showGraph()
{
cout << "Graph is " << endl;
for(int i = 1; i <= vertex_count; i++)
{
cout << i << " -> ";
struct edgelist *current;
current = edges[i];
while(current != NULL)
{
cout << current->value << " ";
current = current->next;
}
cout << endl;
}
}