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

Commit995005c

Browse files
Merge pull requestTheAlgorithms#99 from rei2hu/master
Add adjacency list graph implementation
2 parentsc6887b8 +9b566f9 commit995005c

File tree

1 file changed

+120
-23
lines changed

1 file changed

+120
-23
lines changed

‎data_structures/Graphs/Graphs.java

Lines changed: 120 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,129 @@
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+
* this method removes an edge from the graph between two specified
46+
* verticies
47+
*
48+
* @param from the data of the vertex the edge is from
49+
* @param to the data of the vertex the edge is going to
50+
* @return returns false if the edge doesn't exist, returns true if the edge exists and is removed
51+
*/
52+
publicbooleanremoveEdge(Efrom,Eto) {
53+
VertexfromV =null;
54+
for (Vertexv:verticies) {
55+
if (from.compareTo(v.data) ==0) {
56+
fromV =v;
57+
break;
58+
}
59+
}
60+
if (fromV ==null)returnfalse;
61+
returnfromV.removeAdjacentVertex(to);
62+
}
63+
/**
64+
* this method adds an edge to the graph between two specified
65+
* verticies
66+
*
67+
* @param from the data of the vertex the edge is from
68+
* @param to the data of the vertex the edge is going to
69+
* @return returns true if the edge did not exist, return false if it already did
70+
*/
71+
publicbooleanaddEdge(Efrom,Eto) {
72+
VertexfromV =null,toV =null;
73+
for (Vertexv:verticies) {
74+
if (from.compareTo(v.data) ==0) {// see if from vertex already exists
75+
fromV =v;
76+
}elseif (to.compareTo(v.data) ==0) {// see if to vertex already exists
77+
toV =v;
78+
}
79+
if (fromV !=null &&toV !=null)break;// both nodes exist so stop searching
80+
}
81+
if (fromV ==null) {
82+
fromV =newVertex(from);
83+
verticies.add(fromV);
84+
}
85+
if (toV ==null) {
86+
toV =newVertex(to);
87+
verticies.add(toV);
88+
}
89+
returnfromV.addAdjacentVertex(toV);
90+
}
91+
92+
/**
93+
* this gives a list of verticies in the graph and their adjacencies
94+
*
95+
* @return returns a string describing this graph
96+
*/
97+
publicStringtoString() {
98+
StringBuildersb =newStringBuilder();
99+
for (Vertexv:verticies) {
100+
sb.append("Vertex: ");
101+
sb.append(v.data);
102+
sb.append("\n");
103+
sb.append("Adjacent verticies: ");
104+
for (Vertexv2:v.adjacentVerticies) {
105+
sb.append(v2.data);
106+
sb.append(" ");
107+
}
108+
sb.append("\n");
109+
}
110+
returnsb.toString();
111+
}
112+
}
20113

21114
publicclassGraphs {
22115

23-
/**
24-
* Main method
25-
*
26-
* @param args Command line arguments
27-
*/
28116
publicstaticvoidmain(Stringargs[]) {
29-
30-
}
117+
AdjacencyListGraph<Integer>graph =newAdjacencyListGraph<>();
118+
assertgraph.addEdge(1,2);
119+
assertgraph.addEdge(1,5);
120+
assertgraph.addEdge(2,5);
121+
assert !graph.addEdge(1,2);
122+
assertgraph.addEdge(2,3);
123+
assertgraph.addEdge(3,4);
124+
assertgraph.addEdge(4,1);
125+
assert !graph.addEdge(2,3);
126+
System.out.println(graph);
127+
}
31128

32129
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp