Skip to content

Commit d3d8dc5

Browse files
committed
edded some useful classes
1 parent a5c6001 commit d3d8dc5

6 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎src/algo/1todo‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Prim's
1010
Kruskall
1111
topological sort(all types)
1212
detect cycle in (un)directed graph
13+
detect oddcycle (of each componenent)
1314
Hamiltonian cycle
1415
Hachers graph
1516
vertex cover
@@ -24,5 +25,8 @@ toString
2425
BitSet
2526
BloomFilter
2627

28+
hash, -> encodable, decodable, murmut, sha256
29+
30+
2731

2832

‎src/ds/graph/Edge.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ public Edge(T weight, Vertex from, Vertex to) {
1818
this.to = to;
1919
}
2020

21+
@Override
22+
public boolean equals(Object o) {
23+
if (this == o) return true;
24+
if (o == null || getClass() != o.getClass()) return false;
25+
26+
Edge<?> edge = (Edge<?>) o;
27+
28+
if (weight != null ? !weight.equals(edge.weight) : edge.weight != null) return false;
29+
if (from != null ? !from.equals(edge.from) : edge.from != null) return false;
30+
return !(to != null ? !to.equals(edge.to) : edge.to != null);
31+
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
int result = weight != null ? weight.hashCode() : 0;
37+
result = 31 * result + (from != null ? from.hashCode() : 0);
38+
result = 31 * result + (to != null ? to.hashCode() : 0);
39+
return result;
40+
}
41+
2142
public T getWeight() {
2243
return weight;
2344
}

‎src/ds/graph/Graph.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface Graph<T> {
1111
void removeVertex(T t);
1212
void removeEdge(T t1, T t2);
1313
Collection<Vertex<T>> getVertices();
14+
15+
Collection<Edge<Integer>> getEdges();
1416
int size();
1517
Vertex<T> getVertex(T t);
1618
}

‎src/ds/graph/UndirectedGraph.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ds.graph;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
45
import java.util.HashMap;
56
import java.util.Map;
@@ -8,7 +9,8 @@
89
* Created by sherxon on 1/1/17.
910
*/
1011
public class UndirectedGraph<V> implements Graph<V> {
11-
private Map<V, Vertex<V>> vertexMap= new HashMap<>();
12+
private Map<V, Vertex<V>> vertexMap = new HashMap<>();
13+
private Collection<Edge<Integer>> edges = new ArrayList<>();
1214

1315
@Override
1416
public void addVertex(V v) {
@@ -21,6 +23,10 @@ public Collection<Vertex<V>> getVertices() {
2123
return vertexMap.values();
2224
}
2325

26+
@Override
27+
public Collection<Edge<Integer>> getEdges() {
28+
return edges;
29+
}
2430

2531

2632
@Override
@@ -31,6 +37,7 @@ public void addEdge(V v1, V v2) {
3137
Vertex<V> to=vertexMap.get(v2);
3238
from.addNeighbor(to);
3339
to.addNeighbor(from);
40+
edges.add(new Edge<>(0, from, to));
3441
}
3542

3643
@Override

‎src/ds/graph/Vertex.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ public Vertex(T value) {
1717
this.neighbors= new ArrayList<>();
1818
}
1919

20+
@Override
21+
public boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
25+
Vertex<?> vertex = (Vertex<?>) o;
26+
27+
return value.equals(vertex.value);
28+
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return value.hashCode();
34+
}
35+
2036
public Integer getWeight() {
2137
return weight;
2238
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,81 @@
11
package ds.graph.vertexcover;
22

3+
import ds.graph.Graph;
4+
import ds.graph.UndirectedGraph;
5+
import ds.graph.Vertex;
6+
7+
import java.util.*;
8+
39
/**
410
* Created by sherxon on 3/21/17.
511
*/
612
public class PolynomialSolutionForVertexCover {
13+
public static void main(String[] args) {
14+
15+
int ran = (int) (Math.random() * 100);
16+
System.out.println("number of graphs" + ran);
17+
for (int i = 0; i < ran; i++) {
18+
Graph<Integer> graph = new UndirectedGraph<>();
19+
Graph<Integer> graphForBrute = new UndirectedGraph<>();
20+
int randomVertexNum = (int) (Math.random() * 100);
21+
System.out.println("number of vertices" + randomVertexNum);
22+
for (int j = 0; j < randomVertexNum; j++) {
23+
graph.addVertex(j);
24+
graphForBrute.addVertex(j);
25+
}
26+
27+
int numOfEdge = (int) (Math.random() * randomVertexNum);
28+
System.out.println("number of edges" + numOfEdge);
29+
for (int j = 0; j < numOfEdge; j++) {
30+
int from = (int) (Math.random() * 100);
31+
int to = (int) (Math.random() * 100);
32+
graph.addEdge(from, to);
33+
graphForBrute.addEdge(from, to);
34+
}
35+
Set<Vertex<Integer>> mySolution = getSolution(graph);
36+
Set<Vertex<Integer>> bruteSolution = getSolutionBrute(graphForBrute);
37+
// check solutions
38+
if (mySolution.size() != bruteSolution.size()) {
39+
System.out.println("found a bug");
40+
System.out.println(mySolution);
41+
System.out.println(bruteSolution);
42+
} else {
43+
System.out.println("Size the same!!!!------------------------");
44+
for (Vertex<Integer> integerVertex : mySolution) {
45+
if (!bruteSolution.contains(integerVertex)) {
46+
System.out.println("found a bug here");
47+
System.out.println(integerVertex);
48+
}
49+
}
50+
}
51+
52+
}
53+
}
54+
55+
private static Set<Vertex<Integer>> getSolutionBrute(Graph<Integer> graph) {
56+
Set<Vertex<Integer>> set = new HashSet<>();
57+
List<List<Vertex<Integer>>> powerSet = getPowerSet(graph.getVertices());
58+
Collections.sort(powerSet, (a, b) -> a.size() - b.size());
59+
for (List<Vertex<Integer>> vertexes : powerSet) {
60+
if (isMinVCover(vertexes, graph)) {
61+
return new HashSet<>(vertexes);
62+
}
63+
}
64+
return set;
65+
}
66+
67+
private static List<List<Vertex<Integer>>> getPowerSet(Collection<Vertex<Integer>> vertices) {
68+
return null;
69+
}
70+
71+
private static boolean isMinVCover(List<Vertex<Integer>> vertexes, Graph<Integer> graph) {
72+
Set<Vertex<Integer>> set = new HashSet<>(vertexes);
73+
74+
return false;
75+
}
776

77+
private static Set<Vertex<Integer>> getSolution(Graph<Integer> graph) {
78+
Set<Vertex<Integer>> set = new HashSet<>();
79+
return set;
80+
}
881
}

0 commit comments

Comments
 (0)