-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
43 lines (38 loc) · 1.22 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
#include "Graph.h"
Graph::Graph(int num, bool dir): n(num), hasDir(dir), nodes(num +1) {}
void Graph::addEdge(int src, int dest, int weight) {
if (src < 1 || src > n || dest < 1 || dest > n) {return;}
nodes[src].adj.push_back({dest, weight});
if (!hasDir) {nodes[dest].adj.push_back({src, weight});}
}
list<Flight*> Graph::bfsGetList(int v) {
// initialize all nodes as unvisited
for (int v = 1; v <= n; v++) {nodes[v].visited = false;}
list<Flight*> list1;
queue<int> q; // queue of unvisited nodes
q.push(v);
nodes[v].visited = true;
int i = 1;
while (!q.empty ()) { // while there are still unprocessed nodes
int u = q.front();
q.pop (); // remove first element of q
list1.push_back(nodes[u].flight);
if (i == 1) {
pair<string, int> p;
p.first = nodes[u].flight->getSource()->getCode();
p.second = u;
}
for (auto e : nodes[u].adj) {
int w = e.dest;
if (!nodes[w].visited) { // new node!
q.push(w);
nodes[w].visited = true;
}
}
i++;
}
return list1;
}
void Graph::setFlight(int node, Flight* f) {
nodes[node].flight = f;
}