-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
146 lines (138 loc) · 4.3 KB
/
Graph.cpp
File metadata and controls
146 lines (138 loc) · 4.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <vector>
#include "Graph.h"
using namespace std;
void Graph::addNode(int node){
int out;
out = checkNode(node);
if (out !=1 ){ //adds new node if it doesn't exist.
nodeArray.push_back(node);
cout << "Adding node: " << node << endl;
}
else{
cout<<"The node already exists.\n";
}
}
void Graph::addEdge(int node1, int node2){
int a,check1,check2;
a = checkEdge(node1, node2);
check1=checkNode(node1);
check2=checkNode(node2);
if (a !=1 && check1 ==1 && check2 ==1 ){ //adds edge if the nodes to be connected exist and edge doesn't already exist.
edge1Array.push_back(node1);
edge2Array.push_back(node2);
cout << "Adding edge: (" << node1 <<","<< node2 <<")\n";
}
else if(a==1){
cout<<"The edge already exists.\n";
}
else if(check1 !=1){
cout<<"The node "<< node1 <<" doesn't exist.\n";
}
else{
cout<<"The node "<< node2 <<" doesn't exist.\n";
}
}
void Graph::addUnEdge(int node1, int node2){ //adds undirected edges between two nodes.
addEdge(node1,node2);
addEdge(node2,node1);
}
int Graph::checkNode(int node){ //checks if node exists.
for(int i=0; i< nodeArray.size(); i++){
if (nodeArray[i] == node){
// cout << "The node "<<node<<" exists.\n";
return 1;
}
}
return 0;
}
int Graph::checkEdge(int node1, int node2){ //checks if edge exists.
for(int i=0; i< edge1Array.size(); i++){
if (edge1Array[i] == node1 && edge2Array[i] == node2){
cout<<"The required edge ("<<node1<<","<<node2<<") exists."<<endl;
return 1;
}
}
return 0;
}
void Graph::deleteNode(int node){ //deletes node if it exists.
int out;
out = checkNode(node);
if (out ==1 ){
int count = 0;
for (vector<int>::const_iterator i = nodeArray.begin(); i < (nodeArray.end());i++){ //deletes node from vector
if (*i == node){
nodeArray.erase(nodeArray.begin() + count);
cout<<"Node deleted successfully\n";
}
deleteUnEdge(node,*(nodeArray.begin() + count)); //deletes the edges with this node
if(count<nodeArray.size()-1){ //to delete last node of the array.
count++;
}
}
}
else{
cout<<"The required node doesn't exist.\n";
}
}
void Graph::printNodeList(void){ //prints the nodes.
for (vector<int>::const_iterator i = nodeArray.begin(); i < (nodeArray.end());i++){
cout << "Node :" << (*i) <<endl;
}
}
void Graph::deleteEdge(int node1, int node2){ //delete the edge if it exists.
int a;
a = checkEdge(node1, node2);
if (a ==1 ){
int count = 0;
for (vector<int>::const_iterator i = edge1Array.begin(); i < (edge1Array.end());i++){
if (*(edge1Array.begin() + count) == node1 && *(edge2Array.begin() + count) == node2){
cout<<"Edge ("<<*(edge1Array.begin() + count)<<","<<*(edge2Array.begin() + count)<<") deleted successfully.\n";
edge1Array.erase(edge1Array.begin() + count);
edge2Array.erase(edge2Array.begin() + count);
}
if(count<edge1Array.size()-1){ // to delete the last edge of the array.
count++;
}
}
}
}
void Graph::deleteUnEdge(int node1, int node2){ // delete undirected edges.
deleteEdge(node1,node2);
deleteEdge(node2,node1);
}
void Graph::printEdgeList(){
cout<<"Edge List:\n";
for(int i=0; i< edge1Array.size(); i++){
cout<<"("<<edge1Array[i]<<","<<edge2Array[i]<<")"<<endl;
}
}
void Graph::breadthfirstTraversal(int node){ //bfs to find friends of friends of a node.
int x;
x=checkNode(node);
vector<int> friendArray; // to store the friends of a node.
if(x==1){
int count=0;
cout<<"Friends of "<<node <<":"<<endl;
for (vector<int>::const_iterator i = edge1Array.begin(); i < (edge1Array.end());i++){ //finding friends
if (*((edge1Array.begin() + count)) == node){
friendArray.push_back(*(edge2Array.begin()+count));
cout<<*(edge2Array.begin()+count)<<endl;
}
count++;
}
int count1=0;
cout<<"Friends of friends of "<<node <<" are as follows."<<endl;
for (vector<int>::const_iterator i = friendArray.begin(); i <(friendArray.end());i++){ //finding friends of friends
int count2=0;
cout<<"Friends of "<<*(friendArray.begin()+count1)<<":"<<endl;
for (vector<int>::const_iterator itr = edge1Array.begin(); itr < (edge1Array.end());itr++){
if (*((edge1Array.begin() + count2)) == *(friendArray.begin()+count1) && *(edge2Array.begin()+count2) != node){
cout<<*(edge2Array.begin()+count2)<<endl;
}
count2++;
}
count1++;
}
}
}