-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadGraph.cpp
More file actions
154 lines (154 loc) · 3.86 KB
/
Copy pathReadGraph.cpp
File metadata and controls
154 lines (154 loc) · 3.86 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
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
#include<bits/stdc++.h>
using namespace std;
void Matrix(string node1,string node2,int weight,vector<string>&nodes,vector<vector<int>>&A,int c)
{
int x=-1,y=-1;
for(int i=0;i<nodes.size();i++)
{
if(nodes[i]==node1)
x=i;
if(nodes[i]==node2)
y=i;
if(x!=-1&&y!=-1)
break;
}
if(c==0)
{
A[x][y]=weight;
A[y][x]=weight;
}
else
A[x][y]=weight;
return;
}
struct Graph
{
vector<vector<int>>matrix;
vector<string>nodes;
int C;
vector<string>Dot;
};
Graph CreateGraph()
{
int a=0,b=0,c=0;
cout<<"Reading the input graph:-\n";
string dotpath=/*Enter the full address of the dot file*/"\\Dot Files\\InputGraph.dot";
ifstream file(dotpath);
stringstream buffer;
buffer<<file.rdbuf();
string contents=buffer.str();
if(contents[0]=='d')
c=1;
vector<string>lines;
string line="";
for(int i=0;i<contents.length();i++)
{
if(contents[i]=='}')
break;
if(contents[i]=='\n'&&b<2)
b++;
if(b==2)
{
if(contents[i]=='\t')
continue;
else if(contents[i]=='\n'&&line!="")
{
lines.push_back(line);
line="";
continue;
}
if(contents[i]!='\n')
line=line+contents[i];
}
}
b=0;
for(int i=0;i<lines[0].length();i++)
{
if(lines[0][i]=='[')
{
b=1;
break;
}
}
if(c==0&&b==0)
cout<<"It is an Undirected Unweighted Graph..";
else if(c==0&&b==1)
cout<<"It is an Undirected Weighted Graph..";
else if(c==1&&b==0)
cout<<"It is a Directed Unweighted Graph..";
else
cout<<"It is a Directed Weighted Graph..";
cout<<"\n";
vector<string>nodes;
for(int i=0;i<lines.size();i++)
{
int h;
for(int j=0;j<lines[i].length();j++)
{
string node;
if(lines[i][j]=='-'&&lines[i][j-1]!='-')
{
node=lines[i].substr(0,j-1);
if(find(nodes.begin(),nodes.end(),node)==nodes.end())
nodes.push_back(node);
h=j+3;
}
if(lines[i][j]=='['||(lines[i][j]==';'&&lines[i][j-1]!=']'))
{
node=lines[i].substr(h,j-h);
if(find(nodes.begin(),nodes.end(),node)==nodes.end())
nodes.push_back(node);
break;
}
}
}
sort(nodes.begin(),nodes.end());
cout<<"The nodes present in the graph are:-\n";
for(int i=0;i<nodes.size();i++)
cout<<nodes[i]<<" ";
cout<<"\nThe matrix representation of the graph is:-\n";
a=nodes.size();
vector<vector<int>>A(a,vector<int>(a,0));
for(int i=0;i<lines.size();i++)
{
string node1,node2;
int weight,h;
for(int j=0;j<lines[i].length();j++)
{
if(lines[i][j]=='-'&&lines[i][j-1]!='-')
{
node1=lines[i].substr(0,j-1);
h=j+3;
}
if(lines[i][j]=='['||(lines[i][j]==';'&&lines[i][j-1]!=']'))
{
node2=lines[i].substr(h,j-h);
if(b==0)
{
Matrix(node1,node2,1,nodes,A,c);
break;
}
}
if(lines[i][j]=='=')
h=j+2;
if(lines[i][j]==']')
{
weight=stoi(lines[i].substr(h,j-h-1));
Matrix(node1,node2,weight,nodes,A,c);
break;
}
}
}
for(int i=0;i<a;i++)
{
for(int j=0;j<a;j++)
cout<<A[i][j]<<" ";
cout<<"\n";
}
Graph H;
H.matrix=A;
H.nodes=nodes;
H.C=c;
H.Dot=lines;
return H;
}