-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchar_graph.cpp
More file actions
91 lines (76 loc) · 2.08 KB
/
char_graph.cpp
File metadata and controls
91 lines (76 loc) · 2.08 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Graph with char nodes instead of int
#include<iostream>
#include<map>
#include<bits/stdc++.h>
using namespace std;
char findKeybyVal(map<char,int> mp, int k){
for(auto itr:mp){
if(itr.second == k) return itr.first;
}
return -1;
}
void addEdge(vector<int> adj[], map<char,int> mp, char cu, char cv){
map<char,int>::iterator u = mp.find(cu);
map<char,int>::iterator v = mp.find(cv);
adj[u->second].push_back(v->second);
}
void printGraph(vector<int> adj[], map<char,int> mp, int V){
for (int i = 0; i < V; i++){
cout << findKeybyVal(mp,i) << " ——> "; // print the current vertex number
for (auto v: adj[i])
cout << findKeybyVal(mp,v) << " "; // print all neighboring vertices of a vertex `i`
cout << endl;
}
}
void printMatrix(vector<vector<int>> adjmatrix, map<char,int> mp){
cout << " ";
for(auto itr:mp){
cout << itr.first << " ";
}
cout<<endl;
int V = adjmatrix.size();
for (int i = 0; i < V; i++) {
cout<< findKeybyVal(mp,i) << " ";
for (int j = 0; j < V; j++) {
cout << adjmatrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void convertlist_matrix(vector<int> adj[], map<char,int> mp, int V){
vector<vector<int>> adjmatrix(V,vector<int>(V, 0));
for(int i=0 ;i<V; i++){
for(auto j:adj[i]){
adjmatrix[i][j]=1;
}
}
printMatrix(adjmatrix, mp);
}
int main(){
map<char,int> mp;
map<char,int>::iterator itr;
mp['a']=0;
mp['b']=1;
mp['c']=2;
mp['d']=3;
mp['e']=4;
mp['f']=5;
mp['g']=6;
int V=7;
vector<int> adj[V];
addEdge(adj, mp, 'a', 'b');
addEdge(adj, mp, 'a', 'c');
addEdge(adj, mp, 'b', 'e');
addEdge(adj, mp, 'b', 'g');
addEdge(adj, mp, 'c', 'f');
addEdge(adj, mp, 'd', 'a');
addEdge(adj, mp, 'd', 'b');
addEdge(adj, mp, 'd', 'c');
addEdge(adj, mp, 'd', 'f');
addEdge(adj, mp, 'd', 'g');
addEdge(adj, mp, 'g', 'f');
printGraph(adj, mp, V);
convertlist_matrix(adj,mp,V);
return 0;
}