-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice graphs and trees adjency list.cpp
63 lines (52 loc) · 1.19 KB
/
practice graphs and trees adjency list.cpp
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
#include<iostream>
#include<vector>
using namespace std;
const int N = 10e3+10;
vector<int> graph[N];
vector<pair<int, int> > graph1[N];
int main()
{
int n, m;
cin>>n>>m;
for(int i=0; i<m; i++)
{
int v1, v2, wt;
cin>>v1>>v2>>wt;
graph[v1].push_back(v2);
graph[v2].push_back(v1);
graph1[v1].push_back({v2, wt});
graph1[v2].push_back({v1, wt});
}
// Display the graph
for (int i = 1; i <= n; i++) {
cout << "Adjacent vertices of vertex " << i << ": ";
for (int j = 0; j < graph[i].size(); j++) {
cout << graph[i][j] << " ";
}
cout << endl;
}
cout<<"\nDisplay the weighted graph"<<endl;
// Display the weighted graph
for (int i = 1; i <= n; i++) {
cout << "Adjacent vertices of vertex " << i << ": ";
for (int j = 0; j < graph1[i].size(); j++) {
cout << graph1[i][j].first << " (wt=" << graph1[i][j].second << ") ";
}
cout << endl;
}
// to find the sepcific connection and find weights
for(int child : graph[4])
{
if(child==1)
cout<<"connected!"<<endl;
}
for(pair<int, int> child: graph1[4])
{
if(child.first == 1)
{
cout<<"connected!"<<endl;
cout<<"Weight: "<<child.second<<endl;
}
}
return 0;
}