-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertex.java
More file actions
91 lines (79 loc) · 2.25 KB
/
Vertex.java
File metadata and controls
91 lines (79 loc) · 2.25 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
package structures;
/**
* Adjacency linked lists representation of a vertex in a graph. Also maintains a
* reference to a "parent" vertex, to allow the MST application to keep track of
* partial trees within the graph.
*/
public class Vertex {
/**
* Inner class - represents a neighbor (an outgoing edge) in a weighted graph.
*/
public static class Neighbor {
/**
* Neighboring vertex.
*/
public Vertex vertex;
/**
* Weight of edge to neighbor.
*/
public int weight;
/**
* Next neighbor in the linked list of neighbors.
*/
public Neighbor next;
/**
* Initializes a new Neighbor object.
*
* @param vertex Neighbor vertex
* @param weight Weight of edge to neighbor.
*/
Neighbor(Vertex vertex, int weight) {
this.vertex = vertex;
this.weight = weight;
}
}
/**
* Name of this vertex.
*/
public String name;
/**
* Adjacency linked list of all neighbors.
*/
public Neighbor neighbors;
/**
* Reference to the immediate parent of this vertex in the partial spanning
* tree to which the vertex belongs. Note that a vertex and its parent
* are not necessarily connected by an actual edge of the graph. Even
* if they are directly connected, that edge may not form part of the
* partial tree to which they both belong. The parent reference is only a
* mechanism for determining to WHICH partial tree a vertex belongs.
*/
public Vertex parent;
/**
* Constructs a new Vertex object with no neighbors (i.e.,
* no outgoing edges), and no parent vertex (i.e., it is its own partial
* spanning tree).
* @param name Name to give to this vertex.
*/
Vertex(String name) {
this.name = name;
neighbors = null;
parent = this;
}
/**
* Finds and returns the vertex at the root of the partial spanning tree to
* which this vertex belongs.
* @return Root of partial tree.
*/
public Vertex getRoot() {
Vertex v;
for (v = this ; v.parent != v ; v = v.parent);
return v;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return name;
}
}