-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile
169 lines (154 loc) · 3.71 KB
/
file
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<iostream>
#include<vector>
#include<map>
#include<list>
using namespace std ;
class Edge ;
//class Vertex ;
class Vertex{
private :
int Vid ;
int Vdata ;
int size ;
vector<Edge*> edge;
vector<Vertex> adjecentVertexes;
public :
Vertex(){
}
Vertex(int id){
Vid = id ;
}
int getVertexId(){
return Vid ;
}
void setData(int data){
Vdata = data ;
}
void addAdjecentVertex(Edge* e1 , Vertex v){
cout<<"m in adjeacent "<<endl ;
this->edge.push_back(e1);
this->adjecentVertexes.push_back(v);
}
// void getAllVertex(){
// for(int i = 0 ; i < allVertex.size() ; i++)
// cout<<allVertex[i] <<" ";
// }
};
class Edge{
int Eid;
bool isDirected ;
Vertex v1 ;
Vertex v2 ;
int weight ;
public :
Edge(Vertex v1 , Vertex v2 ){
v1 = v1 ;
v2 = v2 ;
isDirected = false ;
}
Edge(Vertex v1 , Vertex v2 , bool isDirected , int weight){
v1 = v1 ;
v2 = v2 ;
weight = weight ;
isDirected = isDirected ;
}
Edge(Vertex v1 , Vertex v2 , bool isDirected){
v1 = v1 ;
v2 = v2 ;
isDirected = isDirected ;
}
Vertex getVertex1(){
return v1 ;
}
Vertex getVertex2(){
return v2 ;
}
Vertex getWeight(){
return weight ;
}
bool isDirectedEdge(){
return isDirected ;
}
};
class Graph{
private :
vector<Edge*> allEdges ;
map<int,Vertex> allVertex ;
bool isDirected ;
public :
Graph(bool isDirected){
isDirected = isDirected ;
}
void addEdge(int v1 ,int v2 ){
addEdge(v1,v2,0);
}
void addEdge(int v1,int v2, int weight){
cout<<"addEdge to graph"<<endl ;
Vertex vertex1 ;
map<int,Vertex>::iterator it = allVertex.find(v1);
if(it != allVertex.end()){
//Get Id from vertex class
vertex1 = allVertex[v1];
}
else{
allVertex.insert(pair<int,Vertex>(v1,vertex1));
}
Vertex vertex2 ; // = NULL; //new Vertex() ;
map<int,Vertex>::iterator it1 = allVertex.find(v2);
if(it1 != allVertex.end()){
//get id v1 from vertex class
vertex2 = allVertex[v2];
}
else{
allVertex.insert(pair<int,Vertex>(v2,vertex2));
}
Edge* edge = new Edge(v1,v2,isDirected ,weight);
allEdges.push_back(edge);
vertex1.addAdjecentVertex(edge,vertex2);
//After adding it to the edge
// cout<<"After adding it to the edge "<<endl ;
// for(int i = 0 ; allEdges.size() ;i ++)
// cout<<allEdges[i]->getVertexId()<<" " ;
// cout<<endl;
// after adding to it to the vertex class
cout<<"after adding to it to the vertex class "<<endl;
map<int,Vertex>::iterator it3 = allVertex.begin();
if(it3 != allVertex.end()){
cout<<it3->first <<" "<<(it3->second).getVertexId()<<" ";
}
cout<<endl ;
cout<<"aftering printing all the vertex"<<endl ;
//aftering printing all the vertex
cout<<"Checking directed or not "<<endl;
if(!isDirected){
cout<<"Not directed "<<endl;
vertex2.addAdjecentVertex(edge,vertex1) ; //Vertex class ;
}
}
void setDataForVertex(int id, int data){
map<int,Vertex>::iterator it = allVertex.find(id);
if(it != allVertex.end()){
Vertex v1 = allVertex[id];
v1.setData(data);///From vertex class
}
}
void getAllVertex(){
map<int,Vertex>::iterator it = allVertex.begin();
if(it != allVertex.end()){
cout<<it->first <<" ";
}
}
};
int main(){
Graph graph = new Graph(true);
graph.addEdge(1, 3);
graph.addEdge(1, 2);
graph.addEdge(3, 4);
graph.addEdge(5, 6);
graph.addEdge(6, 3);
graph.addEdge(3, 8);
graph.addEdge(8, 11);
cout<<"Graph created "<<endl ;
graph.getAllVertex();
return 0 ;
}