-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatingaGraph.cpp
More file actions
52 lines (37 loc) · 988 Bytes
/
Copy pathCreatingaGraph.cpp
File metadata and controls
52 lines (37 loc) · 988 Bytes
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
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class Graph
{
public:
map<T, vector< pair<T,int> > > adjList;
void addNode(T a, T b,int weight, bool bidirec=false)
{
adjList[a].push_back(make_pair(b,weight));
if(bidirec) adjList[b].push_back(make_pair(a,weight));
}
void printNodes()
{
for(auto node:adjList )
{
cout<<node.first<<"-->";
// vector<pair<T,int>> :: iterator connectedNode;
for(auto connectedNode: node.second)
{
cout<<"{"<<connectedNode.first<<","<<connectedNode.second<<"}"<<",";
}
cout<<endl;
}
}
};
int main()
{
Graph<int> g;
g.addNode(1,2,10,true);
g.addNode(1,3,12);
g.addNode(2,4,14);
g.addNode(3,2,16);
g.addNode(3,4,18,true);
g.printNodes();
return 0;
}