-
Notifications
You must be signed in to change notification settings - Fork 1
/
pathfinder.cpp
96 lines (82 loc) · 2.25 KB
/
pathfinder.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
/**
* Name: Rui Deng
* Dadong Jing
* Data: Mar 10, 2016
* Overview: main program to find shortest path between actors
* Assignment number: PA4
*/
#include <iostream>
#include <fstream>
#include "ActorGraph.hpp"
#include <sstream>
#include <cstring>
using namespace std;
/**
* main method to read and write files and find the shortest paths
* between actors based on the pair file
*/
int main(int argc, char* argv[])
{
//make sure there are 5 arguments:program name, movie cast file,
//weight option, pair file, output file
if(argc < 5)
{
cout<<"number of arguments is incorrect, should be 5"<<endl;
return 0;
}
//load file, read pair file and initialize output stream to write
ActorGraph graph;
graph.loadFromFile(argv[1],false);
ifstream input(argv[3]);
ofstream output(argv[4]);
//check if the files are open
if(input.is_open() == false || output.is_open() == false)
{
cout<<"files are not open"<<endl;
return 0;
}
//initalize some strings used to read file and store actor names
string line;
string actorStart;
string actorEnd;
//read the header of pair file and it is useless
getline(input, line);
//write the first line of output file
output<<"(actor)--[movie#@year]-->(actor)--..."<<endl;
//while loop go through the pair file and find shortest paths
while(1)
{
//get each line containing a pair of actors
getline(input, line);
//end of file, break
if(input.eof())
break;
//using string stream to split the line with tab
istringstream iss(line);
getline(iss, actorStart, '\t');
getline(iss, actorEnd, '\t');
//when choosing unweighted option, using BFS and print path to
//output file
if(strcmp(argv[2],"u") == 0)
{
graph.BFSFind(actorStart, actorEnd);
}
//when choosing weighted option, using dijkstra method
else if(strcmp(argv[2],"w") == 0)
{
graph.DijkstraFind(actorStart, actorEnd);
}
//otherwise wrong input
else
{
cout<<"wrong input option for weighted and unweighted"<<endl;
return 0;
}
//call method to write the path into output file
graph.writePathHelper(actorStart, actorEnd,output);
}
//close files and return
input.close();
output.close();
return 0;
}