Repository navigation
Expand file tree
/
Copy pathGraph.cpp
More file actions
82 lines (72 loc) · 1.38 KB
/
Copy pathGraph.cpp
File metadata and controls
82 lines (72 loc) · 1.38 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<vector>
using namespace std;
class Edge{
public:
int u;
int v;
double weight;
Edge(int u1,int v1){
u = u1;
v = v1;
weight = 0;
}
};
/*Undirected graph class
- contains adjacency Matrix
- Degree of each vertex
- No. of vertices
- No. of edges
- List of edges*/
class Graph{
public:
int **graph; //Adjacency Matrix
int V;
int E;
int* degree;
vector<Edge> edgeList;
Graph(int vertices){
/*Initialising vertices and graph size*/
V = vertices;
E = 0;
degree = new int[V];
graph = new int*[V];
for(int i=0;i<V;i++){
graph[i] = new int[V];
}
/*Initialising graph with no edge i.e. Empty Graph*/
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
graph[i][j]=0;
}
degree[i] = 0;
}
}
/*Insert new Edge and update degree*/
int insertEdge(int u,int v){
if(graph[u][v] != 1 && graph[v][u] != 1){
graph[u][v] = 1;
graph[v][u] = 1;
E++;
degree[u]++;
degree[v]++;
Edge e(u,v);
edgeList.push_back(e);
}
}
/*Adjacency matrix displayed in rows and columns
- Only one cell is updated i.e. if edge is (u,v) then only [u][v] is declared as 1, not [v][u]*/
void displayAdjacencyMatrix(){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
cout<<graph[i][j]<<" ";
}
cout<<"\n";
}
}
void displayEdges(){
for(int i=0;i<edgeList.size();i++){
cout<<edgeList[i].u<<"---"<<edgeList[i].v<<"\n";
}
}
};