-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
152 lines (133 loc) · 3.44 KB
/
Graph.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
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
147
148
149
150
151
152
#include "Graphs.h"
using namespace std;
///////////////////// Path functions
void Path :: enqueue_path(Path p, Node *adding){
// check if the LL is NULL
if(p.vertex == NULL){
p.vertex = adding;
}
else{
//go to the bottom of the LL and add it there
Node *here = p.vertex;
while(here->next!=NULL){
here = here->next;
}
// now we are all the way at the bottom
here->next = adding;
// set the pointer to NULL. we don't need it anymore
here = NULL;
}
}
bool Path :: dequeue_path(Path p, Node *removing){
// check if the LL is already empty
if(p.vertex == NULL){
return false;
}
removing = p.vertex;
Node *newNext = p.vertex->next;
p.vertex = newNext;
removing->next = NULL;
// the removing node is now disconnected from everything
delete(removing);
return true;
}
string Path :: printPath(Path p){
Node *here = p.vertex;
string out = "";
while(here->next!=NULL){
out+= here->actorName + " -"+ here->next->movie + "- ";
here = here->next;
}
// add the last actor
out+=here->actorName;
return out;
}
Node * Path :: last_element(Node *n){
Node *here = n;
while(here->next!=NULL){
here = here->next;
}
return here;
}
///////////////////////// Graph functions
void Graph :: addEdge(Node *start, Node *dest){
//dest goes in g[start]
Node *here = start;
while(here->next!=NULL){
here = here->next;
}
here->next = dest;
}
Node * Graph :: joinNodes(Node *firstlist, Node *secondlist){
Node *here = firstlist;
while(here->next!=NULL){
here = here->next;
}
here->next = secondlist;
}
Node * Graph :: allConnections(Table t, Node *to_find){
string a = to_find->actorName;
string m = to_find->movie;
Node *returning = t.find_actors_from_movie(m);
// we have the cast from movie m
Node *mov = t.find_movies_w_actor(a);
// adding cast from the rest of the movies
while((mov!=NULL)&&(mov->movie!= m)){
string m2 = mov->movie;
Node *add = t.find_actors_from_movie(m2);
returning = joinNodes(returning, add);
mov=mov->next;
}
return returning;
}
bool Graph :: Node_in_LL(Node *comparing, Node *LL){
while(LL!=NULL){
if(LL = comparing){
return true;
}
LL = LL->next;
}
return false;
}
Path * Graph :: bfs(Table t, string actor1, string actor2){
// constructing Node for actor1
Node *abc = t.find_movies_w_actor(actor1);
string m1 = abc->movie;
Node *a1 = new Node(actor1, m1);
// constructing Node for actor2
Node *def = t.find_movies_w_actor(actor2);
string m2 = def->movie;
Node *a2 = new Node(actor2, m2);
// finished constructing Nodes
Path starter = new Path();
starter->vertex = a1;
Node *visited = a1;
vector <Path> to_visit; //LLPath
to_visit.push_back(starter);
while(!to_visit.empty()){
Path current = to_visit.begin();
to_visit.erase(to_visit.begin()); // dequeues Path from LL
Node *edge = last_element(current.vertex);
// compare edge to a2
if(edge == a2){ // this most likely will not be the case
return current;
}
// go through neighbors
Node *neighbors = allConnections(t, edge);
while(neighbors!=NULL){
if(!Node_in_LL(neighbors, visited)){
Path queueing = current;
enqueue_path(queueing, neighbors); // the Path is extended
to_visit.push_back(queueing); // add this extended Path to the queue of Paths
visited->next = neighbors;
}
neighbors = neighbors->next;
}
if(!Node_in_LL(edge, visited)){
visited->next = edge;
}
}
// if it has yet to return anything, return an empty Path
Path empty = new Path();
return empty;
}