Skip to content

Commit 7859670

Browse files
committed
Added Representation of graphs
1 parent 117e0ef commit 7859670

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
int main(int argc, char const *argv[])
7+
{
8+
int n, m;
9+
cin >> n >> m;
10+
11+
vector<int> adj_list[n + 1];
12+
13+
while (m--)
14+
{
15+
int u, v;
16+
cin >> u >> v;
17+
18+
adj_list[u].push_back(v);
19+
adj_list[v].push_back(u);
20+
21+
}
22+
23+
for(int i=1; i<n+1; i++) {
24+
for(int j=0; j<adj_list[i].size(); j++)
25+
cout << adj_list[i][j] << " ";
26+
cout << "\n";
27+
}
28+
29+
return 0;
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main(int argc, char const *argv[])
6+
{
7+
int n, m;
8+
cin >> n >> m;
9+
10+
int adj[n + 1][n + 1];
11+
12+
while (m--)
13+
{
14+
int u, v;
15+
cin >> u >> v;
16+
adj[u][v] = 1;
17+
adj[v][u] = 1;
18+
}
19+
for (int i = 0; i < n + 1; i++)
20+
{
21+
for (int j = 0; j < n + 1; j++)
22+
{
23+
cout << adj[i][j] << "\t\t";
24+
}
25+
cout << "\n";
26+
}
27+
return 0;
28+
}

0 commit comments

Comments
 (0)