Skip to content

Commit 199d4ff

Browse files
author
khalil2535
committed
update
1 parent dd79913 commit 199d4ff

File tree

3 files changed

+97
-27
lines changed

3 files changed

+97
-27
lines changed

data_structures/graph/UnDirectedGraph.java

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010
public class UnDirectedGraph<E> {
1111

12-
ArrayList<vertix<E>> vertixes;
12+
ArrayList<Vertex<E>> vertexes;
1313

1414
public UnDirectedGraph() {
15-
this.vertixes = new ArrayList<>();
15+
this.vertexes = new ArrayList<>();
1616
}
1717

1818
/**
@@ -22,7 +22,7 @@ public UnDirectedGraph() {
2222
*/
2323
public boolean addEntity(E entity) {
2424
if (!contatin(entity)) {
25-
return vertixes.add(new vertix<>(entity));
25+
return vertexes.add(new Vertex<>(entity));
2626
} else {
2727
return false;
2828
}
@@ -32,11 +32,12 @@ public boolean addEntity(E entity) {
3232
*
3333
* @param entity1
3434
* @param entity2
35+
* @param space
3536
* @return true if added or false if not
3637
*/
37-
public boolean addConnection(E entity1, E entity2) {
38+
public boolean addConnection(E entity1, E entity2, int space) {
3839
if (contatin(entity1) && contatin(entity2)) {
39-
return vertixOf(entity1).connect(vertixOf(entity2));
40+
return vertexOf(entity1).connect(vertexOf(entity2), space);
4041
}
4142
return false;
4243
}
@@ -47,53 +48,90 @@ public boolean addConnection(E entity1, E entity2) {
4748
* @return true if contains or false if not
4849
*/
4950
public boolean contatin(E entity) {
50-
return vertixes.stream().anyMatch((v) -> (v.item == entity));
51+
return vertexes.stream().anyMatch((v) -> (v.item == entity));
5152
}
5253

5354
/**
5455
*
5556
* @param entity
56-
* @return the vertix for the entity
57+
* @return the Vertex for the entity
5758
*/
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;
6263
}
6364
}
6465
return null;
6566
}
6667

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> {
68102

69103
E item;
70-
ArrayList<vertix<E>> connections;
104+
ArrayList<Object[]> connections;
71105

72-
public vertix(E entity) {
106+
public Vertex(E entity) {
73107
this.item = entity;
74108
connections = new ArrayList<>();
75109
}
76110

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)) {
79113
return false;
80-
} else {
81-
connections.add(v);
82-
v.connections.add(this);
83-
return true;
84114
}
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;
85124
}
86125

87126
@Override
88127
public String toString() {
89128
return item.toString();
90129
}
91130

92-
}
131+
public boolean inRealtionWith(Vertex<E> v) {
132+
return connections.stream().anyMatch((connection) -> (connection[0] == v));
133+
}
93134

94-
@Override
95-
public String toString() {
96-
return vertixes.toString();
97135
}
98136

99137
}

data_structures/graph/tests/GraphTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,36 @@ public static void main(String[] args) {
1212
UnDirectedGraph g = new UnDirectedGraph();
1313
Object A = "A";
1414
Object B = "B";
15+
Object C = "C";
16+
Object D = "D";
17+
Object E = "E";
18+
Object F = "F";
19+
Object G = "G";
20+
Object H = "H";
21+
Object I = "I";
22+
Object J = "J";
1523
g.addEntity(A);
1624
g.addEntity(B);
17-
g.addConnection(A, B);
25+
g.addEntity(C);
26+
g.addEntity(D);
27+
g.addEntity(E);
28+
g.addEntity(F);
29+
g.addEntity(G);
30+
g.addEntity(H);
31+
g.addEntity(I);
32+
g.addEntity(J);
33+
g.addConnection(A, B, 1);
34+
g.addConnection(A, D, 1);
35+
g.addConnection(A, C, 1);
36+
g.addConnection(A, E, 1);
37+
g.addConnection(B, C, 1);
38+
g.addConnection(B, F, 1);
39+
g.addConnection(C, D, 1);
40+
g.addConnection(F, H, 1);
41+
g.addConnection(D, G, 1);
42+
g.addConnection(G, I, 1);
1843
System.out.println(g);
44+
System.out.println(g.dfs());
1945

2046
}
2147

data_structures/stacks/Stack.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package data_structures.stacks;
22

3+
import java.util.Arrays;
4+
35
public class Stack<T> {
46

5-
private T items[];
7+
private final T items[];
68
int pointer;
79

810
public Stack(int capacity) {
@@ -35,7 +37,7 @@ public T pop() {
3537
}
3638
}
3739

38-
public T showTop() {
40+
public T peek() {
3941
if (!isEmpty()) {
4042
return items[pointer];
4143
} else {
@@ -47,6 +49,10 @@ public int size() {
4749
return pointer + 1;
4850
}
4951

52+
public int indexOf(T item) {
53+
return Arrays.binarySearch(items, item);
54+
}
55+
5056
@Override
5157
public String toString() {
5258
String stack = "------------\n";

0 commit comments

Comments
 (0)