Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit381d898

Browse files
committed
add adjacency list implementation for graphs
1 parent230a005 commit381d898

File tree

2 files changed

+116
-23
lines changed

2 files changed

+116
-23
lines changed

‎data_structures/Graphs/Graphs.java

Lines changed: 113 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,122 @@
1+
importjava.util.ArrayList;
2+
importjava.lang.StringBuilder;
13

2-
/**
3-
* This class implements the Graph data structure
4-
* using the classes Graph and Graphs.
5-
*
6-
* @author Zachary Jones
7-
*
8-
*/
9-
classGraph {
4+
classAdjacencyListGraph<EextendsComparable<E>> {
105

11-
}
6+
ArrayList<Vertex>verticies;
7+
8+
publicAdjacencyListGraph() {
9+
verticies =newArrayList<>();
10+
}
11+
12+
privateclassVertex {
13+
Edata;
14+
ArrayList<Vertex>adjacentVerticies;
15+
16+
publicVertex(Edata) {
17+
adjacentVerticies =newArrayList<>();
18+
this.data =data;
19+
}
1220

13-
/**
14-
* This class is used to test the Graph
15-
* class above.
16-
*
17-
* @author Zachary Jones
18-
*
19-
*/
21+
publicbooleanaddAdjacentVertex(Vertexto) {
22+
for (Vertexv:adjacentVerticies) {
23+
if (v.data.compareTo(to.data) ==0) {
24+
returnfalse;// the edge already exists
25+
}
26+
}
27+
returnadjacentVerticies.add(to);// this will return true;
28+
}
29+
30+
publicbooleanremoveAdjacentVertex(Eto) {
31+
// use indexes here so it is possible to
32+
// remove easily without implementing
33+
// equals method that ArrayList.remove(Object o) uses
34+
for (inti =0;i <adjacentVerticies.size();i++) {
35+
if (adjacentVerticies.get(i).data.compareTo(to) ==0) {
36+
adjacentVerticies.remove(i);
37+
returntrue;
38+
}
39+
}
40+
returnfalse;
41+
}
42+
}
43+
44+
/**
45+
* @param from the data of the vertex the edge is from
46+
* @param to the data of the vertex the edge is going to
47+
* @return returns false if the edge doesn't exist, returns true if the edge exists and is removed
48+
*/
49+
publicbooleanremoveEdge(Efrom,Eto) {
50+
VertexfromV =null;
51+
for (Vertexv:verticies) {
52+
if (from.compareTo(v.data) ==0) {
53+
fromV =v;
54+
break;
55+
}
56+
}
57+
if (fromV ==null)returnfalse;
58+
returnfromV.removeAdjacentVertex(to);
59+
}
60+
/**
61+
* @param from the data of the vertex the edge is from
62+
* @param to the data of the vertex the edge is going to
63+
* @return returns true if the edge did not exist, return false if it already did
64+
*/
65+
publicbooleanaddEdge(Efrom,Eto) {
66+
VertexfromV =null,toV =null;
67+
for (Vertexv:verticies) {
68+
if (from.compareTo(v.data) ==0) {// see if from vertex already exists
69+
fromV =v;
70+
}elseif (to.compareTo(v.data) ==0) {// see if to vertex already exists
71+
toV =v;
72+
}
73+
if (fromV !=null &&toV !=null)break;// both nodes exist so stop searching
74+
}
75+
if (fromV ==null) {
76+
fromV =newVertex(from);
77+
verticies.add(fromV);
78+
}
79+
if (toV ==null) {
80+
toV =newVertex(to);
81+
verticies.add(toV);
82+
}
83+
returnfromV.addAdjacentVertex(toV);
84+
}
85+
86+
/**
87+
*
88+
* @return returns a string describing this graph
89+
*/
90+
publicStringtoString() {
91+
StringBuildersb =newStringBuilder();
92+
for (Vertexv:verticies) {
93+
sb.append("Vertex: ");
94+
sb.append(v.data);
95+
sb.append("\n");
96+
sb.append("Adjacent verticies: ");
97+
for (Vertexv2:v.adjacentVerticies) {
98+
sb.append(v2.data);
99+
sb.append(" ");
100+
}
101+
sb.append("\n");
102+
}
103+
returnsb.toString();
104+
}
105+
}
20106

21107
publicclassGraphs {
22108

23-
/**
24-
* Main method
25-
*
26-
* @param args Command line arguments
27-
*/
28109
publicstaticvoidmain(Stringargs[]) {
29-
30-
}
110+
AdjacencyListGraph<Integer>graph =newAdjacencyListGraph<>();
111+
assertgraph.addEdge(1,2);
112+
assertgraph.addEdge(1,5);
113+
assertgraph.addEdge(2,5);
114+
assert !graph.addEdge(1,2);
115+
assertgraph.addEdge(2,3);
116+
assertgraph.addEdge(3,4);
117+
assertgraph.addEdge(4,1);
118+
assert !graph.addEdge(2,3);
119+
System.out.println(graph);
120+
}
31121

32122
}

‎data_structures/Graphs/makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
javac Graphs.java
3+
java -ea Graphs

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp