-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph array.cpp
53 lines (39 loc) · 1.08 KB
/
Graph array.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
#include<iostream>
#include<map>
#include<vector>
using namespace std;
// Array implementation of Graph
void printAdjMatrix(int n , map<int,int>mp){
// Created a nxn matrix with all zeros
vector<vector<int>> adjMatrix(n,vector<int>(n,0));
for(const auto& pair : mp){
adjMatrix[pair.first][pair.second] = 1;
}
cout << "Adjancency matrix representation of given graph is : \n\n";
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << adjMatrix[i][j] << " " ;
}cout << endl;
}
return;
}
int main(){
map<int,int>mp;
// Take Input from user number of nodes and number of edges
int N;
cout << "Enter number of nodes: " ;
cin >> N ;
int E;
cout << "Enter number of edges : " ;
cin >> E ;
cout << "Enter connections to be made : \n" ;
for(int i=1;i<=E;i++){
int firstNode,secondNode;
cout << "Enter connection " << i << " : ";
cin >> firstNode >> secondNode;
mp[firstNode] = secondNode;
}
printAdjMatrix(N,mp);
cout << endl;
return 0;
}