-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphnode.java
More file actions
148 lines (134 loc) · 3.4 KB
/
Graphnode.java
File metadata and controls
148 lines (134 loc) · 3.4 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
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
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Main Class File: FollowsAnalysis.java
// File: Graphnode.java
// Semester: Spring 2011
//
// Author: Erin Rasmussen
// CS Login: rasmusse
// Lecturer's Name: Beck Hasti
// Lab Section: Lecture 2
//////////////////////////// 80 columns wide //////////////////////////////////
import java.util.*;
/**
* This class represents the individual graphnodes of the graph. Each node
* holds a string which is the name of a user in a twitter network and which
* other graphnodes the node is connected to.
*
* <p>Bugs: none known
*
* @author Erin Rasmussen
*/
public class Graphnode implements Comparable<Graphnode> {
private TreeSet<String> successors;
private int inDegree;
private String label;
Iterator<String> iter;
private boolean visited;
private int distance;
/**
*The constructor for each individual graphnode.
* @param label the name of the graphnode to be created
*/
public Graphnode(String label){
successors = new TreeSet<String>();
this.inDegree = 0;
this.label = label;
this.visited = false;
this.distance = 0;
}
/**
*Sets the visited value of the graphnode to the given boolean value
* @param b the true/false value of the node's visited property
*/
public void setVisited(boolean b){
this.visited = b;
}
/**
* Returns whether a node has been visited or not
* @return true if visited already, false if not
*/
public boolean getVisited(){
return visited;
}
/**
* Sets the distance from the "root" node for the shortestPath method.
* @param distance the distance away from the "root" node
*/
public void setDistance(int distance){
this.distance = distance;
}
/**
*Sets the distance of a node to 0
*/
public void resetDistance(){
distance = 0;
}
/**
*Returns the distance of a node from a "root" node
* @return the node's distance
*/
public int getDistance(){
return distance;
}
/**
*Adds and edge from the graphnode to the node with the name given
* @param label the name of the follower of the user
*/
public void addEdge(String label){
if(!containsSuccessors(label))successors.add(label);
}
/**
*Returns the graphnode's user name.
* @return the name of a user in the graph
*/
public String getName(){
return label;
}
/**
*Increases the in-degree of a node by one.
*/
public void setInDegree(){
inDegree++;
}
/**
*Returns the in-degree of a node.
* @return the in-degree of a node
*/
public int getInDegree(){
return inDegree;
}
/**
*Returns the out-degree of a node
* @return the out-degree of a node
*/
public int getOutDegree(){
return getSuccessors().size();
}
/**
*Returns all successors of the node, a TreeSet of followers
*of the user.
* @return a TreeSet of all followers of the user
*/
public TreeSet<String> getSuccessors(){
return successors;
}
/**
*Returns true if a user has a certain follower
* @param g the name of the follower to search for
* @return true if the user is a follower, false if not
*/
public boolean containsSuccessors(String g){
iter = successors.iterator();
String tmp;
while (iter.hasNext()){
tmp = iter.next();
if (tmp.equals(g))return true;
}
return false;
}
@Override
public int compareTo(Graphnode other) {
return label.compareTo(other.getName());
}
}