Skip to content

Commit dd79913

Browse files
author
khalil2535
committed
added Graph
1 parent 65c9fce commit dd79913

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package data_structures.graph;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
*
7+
* @author khalil2535
8+
* @param <E>
9+
*/
10+
public class UnDirectedGraph<E> {
11+
12+
ArrayList<vertix<E>> vertixes;
13+
14+
public UnDirectedGraph() {
15+
this.vertixes = new ArrayList<>();
16+
}
17+
18+
/**
19+
*
20+
* @param entity
21+
* @return true if added or false if not
22+
*/
23+
public boolean addEntity(E entity) {
24+
if (!contatin(entity)) {
25+
return vertixes.add(new vertix<>(entity));
26+
} else {
27+
return false;
28+
}
29+
}
30+
31+
/**
32+
*
33+
* @param entity1
34+
* @param entity2
35+
* @return true if added or false if not
36+
*/
37+
public boolean addConnection(E entity1, E entity2) {
38+
if (contatin(entity1) && contatin(entity2)) {
39+
return vertixOf(entity1).connect(vertixOf(entity2));
40+
}
41+
return false;
42+
}
43+
44+
/**
45+
*
46+
* @param entity
47+
* @return true if contains or false if not
48+
*/
49+
public boolean contatin(E entity) {
50+
return vertixes.stream().anyMatch((v) -> (v.item == entity));
51+
}
52+
53+
/**
54+
*
55+
* @param entity
56+
* @return the vertix for the entity
57+
*/
58+
private vertix<E> vertixOf(E entity) {
59+
for (vertix<E> vertix : vertixes) {
60+
if (vertix.item == entity) {
61+
return vertix;
62+
}
63+
}
64+
return null;
65+
}
66+
67+
class vertix<E> {
68+
69+
E item;
70+
ArrayList<vertix<E>> connections;
71+
72+
public vertix(E entity) {
73+
this.item = entity;
74+
connections = new ArrayList<>();
75+
}
76+
77+
public boolean connect(vertix<E> v) {
78+
if (connections.contains(v)) {
79+
return false;
80+
} else {
81+
connections.add(v);
82+
v.connections.add(this);
83+
return true;
84+
}
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return item.toString();
90+
}
91+
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return vertixes.toString();
97+
}
98+
99+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package data_structures.graph.tests;
2+
3+
import data_structures.graph.UnDirectedGraph;
4+
5+
/**
6+
*
7+
* @author khalil2535
8+
*/
9+
public class GraphTest {
10+
11+
public static void main(String[] args) {
12+
UnDirectedGraph g = new UnDirectedGraph();
13+
Object A = "A";
14+
Object B = "B";
15+
g.addEntity(A);
16+
g.addEntity(B);
17+
g.addConnection(A, B);
18+
System.out.println(g);
19+
20+
}
21+
22+
}

data_structures/queues/Queue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Queue<T> {
77
private int nitems;
88
private int rear;
99
private int front;
10-
private T[] array;
10+
private final T[] array;
1111

1212
public Queue(int capacity) {
1313
array = (T[]) new Object[capacity];

0 commit comments

Comments
 (0)