forked from rudrasohan/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weight.cpp
71 lines (59 loc) · 1.56 KB
/
weight.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
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
// Number of vertices in the graph
#define N 6
// data structure to store graph edges
struct Edge {
int src, dest;
};
// class to represent a graph object
class Graph
{
public:
// A array of vectors to represent adjacency list
vector<int> adjList[N];
// Constructor
Graph(vector<Edge> edges)
{
// add edges to the undirected graph
for (unsigned i = 0; i < edges.size(); i++)
{
int src = edges[i].src;
int dest = edges[i].dest;
// insert at end
adjList[src].push_back(dest);
// Uncomment below line for undirected graph
// adjList[dest].push_back(src);
}
}
};
// print adjacency list representation of graph
void printGraph(Graph const& graph)
{
for (int i = 0; i < N; i++)
{
// print current vertex number
cout << i << " --> ";
// print all neighboring vertices of vertex i
for (int v : graph.adjList[i])
cout << v << " ";
cout << endl;
}
}
// main function
int main()
{
// vector of graph edges as per above diagram. Please
// note that initialization vector in below format will
// work fine in C++11 and C++14 but will fail in C++98.
vector<Edge> edges =
{
{ 0, 1 }, { 1, 2 }, { 2, 0 }, { 2, 1 },
{ 3, 2 }, { 4, 5 }, { 5, 4 }
};
// construct graph
Graph graph(edges);
// print adjacency list representation of graph
printGraph(graph);
return 0;
}