9
9
*/
10
10
public class UnDirectedGraph <E > {
11
11
12
- ArrayList <vertix <E >> vertixes ;
12
+ ArrayList <Vertex <E >> vertexes ;
13
13
14
14
public UnDirectedGraph () {
15
- this .vertixes = new ArrayList <>();
15
+ this .vertexes = new ArrayList <>();
16
16
}
17
17
18
18
/**
@@ -22,7 +22,7 @@ public UnDirectedGraph() {
22
22
*/
23
23
public boolean addEntity (E entity ) {
24
24
if (!contatin (entity )) {
25
- return vertixes .add (new vertix <>(entity ));
25
+ return vertexes .add (new Vertex <>(entity ));
26
26
} else {
27
27
return false ;
28
28
}
@@ -32,11 +32,12 @@ public boolean addEntity(E entity) {
32
32
*
33
33
* @param entity1
34
34
* @param entity2
35
+ * @param space
35
36
* @return true if added or false if not
36
37
*/
37
- public boolean addConnection (E entity1 , E entity2 ) {
38
+ public boolean addConnection (E entity1 , E entity2 , int space ) {
38
39
if (contatin (entity1 ) && contatin (entity2 )) {
39
- return vertixOf (entity1 ).connect (vertixOf (entity2 ));
40
+ return vertexOf (entity1 ).connect (vertexOf (entity2 ), space );
40
41
}
41
42
return false ;
42
43
}
@@ -47,53 +48,90 @@ public boolean addConnection(E entity1, E entity2) {
47
48
* @return true if contains or false if not
48
49
*/
49
50
public boolean contatin (E entity ) {
50
- return vertixes .stream ().anyMatch ((v ) -> (v .item == entity ));
51
+ return vertexes .stream ().anyMatch ((v ) -> (v .item == entity ));
51
52
}
52
53
53
54
/**
54
55
*
55
56
* @param entity
56
- * @return the vertix for the entity
57
+ * @return the Vertex for the entity
57
58
*/
58
- private vertix <E > vertixOf (E entity ) {
59
- for (vertix <E > vertix : vertixes ) {
60
- if (vertix .item == entity ) {
61
- return vertix ;
59
+ private Vertex <E > vertexOf (E entity ) {
60
+ for (Vertex <E > vertex : vertexes ) {
61
+ if (vertex .item == entity ) {
62
+ return vertex ;
62
63
}
63
64
}
64
65
return null ;
65
66
}
66
67
67
- class vertix <E > {
68
+ /**
69
+ *
70
+ * @deprecated not supported yet
71
+ */
72
+ public ArrayList <E > dfs () {
73
+ ArrayList <Vertex <E >> unVisitedVertexes = (ArrayList <Vertex <E >>) vertexes .clone ();
74
+ ArrayList <E > visited = new ArrayList <>();
75
+ Vertex <E > first = vertexes .get (0 );// adding first item
76
+ visited .add (first .item );
77
+ Vertex <E > temp = first ;
78
+ // start dfs
79
+ recursiveInnerDFS (visited , unVisitedVertexes , temp );
80
+ // end
81
+ return visited ;
82
+ }
83
+
84
+ private void recursiveInnerDFS (ArrayList <E > visited , ArrayList <Vertex <E >> unVisited , Vertex <E > v ) {
85
+ v .connections .stream ().map ((connection ) -> (Vertex <E >) connection [0 ]).forEachOrdered ((temp ) -> {
86
+ if (!visited .contains (temp .item )) {
87
+ visited .add (temp .item );
88
+ unVisited .remove (temp );
89
+ } else {
90
+ recursiveInnerDFS (visited , unVisited , temp );
91
+ }
92
+ });
93
+
94
+ }
95
+
96
+ @ Override
97
+ public String toString () {
98
+ return vertexes .toString ();
99
+ }
100
+
101
+ class Vertex <E > {
68
102
69
103
E item ;
70
- ArrayList <vertix < E > > connections ;
104
+ ArrayList <Object [] > connections ;
71
105
72
- public vertix (E entity ) {
106
+ public Vertex (E entity ) {
73
107
this .item = entity ;
74
108
connections = new ArrayList <>();
75
109
}
76
110
77
- public boolean connect (vertix <E > v ) {
78
- if (connections . contains (v )) {
111
+ public boolean connect (Vertex <E > v , int space ) {
112
+ if (inRealtionWith (v )) {
79
113
return false ;
80
- } else {
81
- connections .add (v );
82
- v .connections .add (this );
83
- return true ;
84
114
}
115
+ Object [] a1 = new Object [2 ];
116
+ a1 [0 ] = v ;
117
+ a1 [1 ] = space ;
118
+ connections .add (a1 );
119
+ Object [] a2 = new Object [2 ];
120
+ a2 [0 ] = this ;
121
+ a2 [1 ] = space ;
122
+ v .connections .add (a2 );
123
+ return true ;
85
124
}
86
125
87
126
@ Override
88
127
public String toString () {
89
128
return item .toString ();
90
129
}
91
130
92
- }
131
+ public boolean inRealtionWith (Vertex <E > v ) {
132
+ return connections .stream ().anyMatch ((connection ) -> (connection [0 ] == v ));
133
+ }
93
134
94
- @ Override
95
- public String toString () {
96
- return vertixes .toString ();
97
135
}
98
136
99
137
}
0 commit comments