-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
100 lines (94 loc) · 2.69 KB
/
Graph.h
File metadata and controls
100 lines (94 loc) · 2.69 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
#ifndef GRAPH_H
#define GRAPH_H
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include"Website.h"
using namespace std;
class Graph
{
private:
int numOfVertices;
vector<int> *adjacencyList;
public:
Graph(int n)
{
numOfVertices = n;
adjacencyList = new vector<int>[numOfVertices];
}
void addEdge(website* sites,int n) //here it creates the directed webgraph using the adjacency list
{
vector<int> indexes;
string temp;
fstream file;
file.open("webgraph.csv");
while(!file.eof())
{
int flag = 0;
file>>temp;
stringstream split(temp);
string data;
while (split.good())
{
string data;
getline(split, data, ',');
for(int i=0; i<n; i++)
{
if(data==sites[i].checkLink())
{
indexes.push_back(sites[i].vNum()); //it stores the indexes by order in the vector calles indexes
}
}
}
}
file.close();
vector<int>::iterator it = indexes.begin();
while(it!=indexes.end())
{
adjacencyList[*it].push_back(*(it+1)); //here, all indexes are added to the adjacency list with the right order to form a directed graph
it=it+2;
}
}
void displayGraph(int total)
{
for(int i=0; i<total; i++)
{
cout<<"Vertex no. "<<i;
vector<int>::iterator it=adjacencyList[i].begin();
while(it!=adjacencyList[i].end())
{
cout<<"->"<<*it;
it++;
}
cout<<endl;
}
}
double outgoing(int index, int* to,double cTotal)
{
double counter=0;
vector<int>::iterator it = adjacencyList[index].begin();
int i=0;
while(it!=adjacencyList[index].end()) //here i count all outgoing edges from a certain node, and save the value for each node;
{
to[i] = *it;
counter++;
i++;
it++;
}
if(counter==0) //if there exists dangling nodes, i must assume that it is connected to every other nodes and also to itself so that page rank is calculated in a right way;
{
counter=cTotal;
for(int i = 0; i<cTotal; i++)
{
to[i]=i;
}
return counter;
}
else
{
return counter;
}
}
};
#endif