-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWebGraph.java
160 lines (124 loc) · 3.73 KB
/
MyWebGraph.java
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
153
154
155
156
157
158
159
160
package finalproject;
import java.util.ArrayList;
import java.util.HashMap;
public class MyWebGraph {
HashMap<String, WebVertex> vertexList;
public MyWebGraph () {
vertexList = new HashMap<String, WebVertex>();
}
/*
* adds a vertex given a url
* returns true if the graph has changed as a result of this operation
* false otherwise.
*/
public boolean addVertex(String s) {
// add a vertex to the graph if it's not there yet
if (! vertexList.containsKey(s)) {
WebVertex v = new WebVertex(s);
vertexList.put(s, v);
return true;
}
return false;
}
/*
* add an edge between two vertices.
* returns true if the graph has changed as a result of this operation
* false otherwise.
*/
public boolean addEdge(String s, String t) {
if (vertexList.containsKey(s) && vertexList.containsKey(t)) {
WebVertex v = vertexList.get(s);
return v.addEdge(t);
}
return false;
}
// Returns an ArrayList of urls that are neighbors with the given url
public ArrayList<String> getNeighbors(String url) {
return vertexList.get(url).getNeighbors();
}
// Returns a list of all urls in the graph
public ArrayList<String> getVertices() {
ArrayList<String> urls = new ArrayList<String>();
for (String url: vertexList.keySet())
urls.add(url);
return urls;
}
// Returns the list of pages that have links to v
public ArrayList<String> getEdgesInto(String v) {
ArrayList<String> linksInto = new ArrayList<String>();
for (String url: vertexList.keySet()) {
WebVertex page = vertexList.get(url);
if (page.containsEdge(v))
linksInto.add(page.url);
}
return linksInto;
}
// Returns the number of links in the page with the specified url
int getOutDegree(String url) {
// NullPointerException raised if there's no vertex with specified url
return vertexList.get(url).links.size();
}
// returns the page rank of a given url. If the vertex with the specified url doesn't exist, returns 0
double getPageRank(String url) {
if (vertexList.containsKey(url))
return (vertexList.get(url)).rank;
return 0;
}
// sets the pageRank of a given url
void setPageRank(String url, double pr) {
vertexList.get(url).rank = pr;
}
// returns the visited status of a given url
boolean getVisited(String url) {
if (vertexList.containsKey(url))
return (vertexList.get(url)).visited;
return false;
}
// sets the visited status of a given url
boolean setVisited(String url, boolean b) {
if (vertexList.containsKey(url)) {
(vertexList.get(url)).visited = b;
return true;
}
return false;
}
public String toString() {
String info = "";
for (String s: vertexList.keySet()) {
info += s.toString() + "\n";
}
return info;
}
class WebVertex {
private String url;
private ArrayList<String> links;
private boolean visited;
private double rank;
/*
* Creates a vertex given a url.
* This vertex has no edges yet.
*/
WebVertex (String url) {
this.url = url;
this.links = new ArrayList<String>();
this.visited = false;
this.rank = 0;
}
boolean addEdge(String v) {
if (!this.links.contains(v)) {
this.links.add(v);
return true;
}
return false;
}
public ArrayList<String> getNeighbors() {
return this.links;
}
boolean containsEdge(String e) {
return this.links.contains(e);
}
public String toString() {
return this.url + "\t" + this.visited + "\t" + this.rank;
}
}
}