Java Reference
In-Depth Information
74
75 @Override
/** Return the index for the specified vertex object */
76
public int
getIndex(V v) {
getIndex
77
return
vertices.indexOf(v);
78 }
79
80 @Override
/** Return the neighbors of the specified vertex */
81
public
List<Integer> getNeighbors(
int
index) {
82 List<Integer> result =
new
ArrayList<>();
83
for
(Edge e: neighbors.get(index))
84 result.add(e.v);
85
86
getNeighbors
return
result;
87 }
88
89 @Override
/** Return the degree for a specified vertex */
90
public int
getDegree(
int
v) {
getDegree
91
return
neighbors.get(v).size();
92 }
93
94 @Override
/** Print the edges */
95
public void
printEdges() {
96
for
(
int
u =
0
; u < neighbors.size(); u++) {
97 System.out.print(getVertex(u) +
" ("
+ u +
"): "
);
98
for
(Edge e: neighbors.get(u)) {
99 System.out.print(
"("
+ getVertex(e.u) +
", "
+
100 getVertex(e.v) +
") "
);
101 }
102 System.out.println();
103 }
104 }
105
106 @Override
/** Clear the graph */
107
public void
clear() {
108 vertices.clear();
109 neighbors.clear();
110 }
111
112 @Override
/** Add a vertex to the graph */
113
public boolean
addVertex(V vertex) {
114
if
(!vertices.contains(vertex)) {
115 vertices.add(vertex);
116 neighbors.add(
new
ArrayList<Edge>());
117
printEdges
clear
addVertex
return true
;
118 }
119
else
{
120
return false
;
121 }
122 }
123
124
/** Add an edge to the graph */
125
protected boolean
addEdge(Edge e) {
addEdge
126
if
(e.u <
0
|| e.u > getSize() -
1
)
127
throw new
IllegalArgumentException(
"No such index: "
+ e.u);
128
129
if
(e.v <
0
|| e.v > getSize() -
1
)
130
throw new
IllegalArgumentException(
"No such index: "
+ e.v);
131
132
if
(!neighbors.get(e.u).contains(e)) {
133 neighbors.get(e.u).add(e);
Search WWH ::
Custom Search