Skip to content

Commit

Permalink
Updated graph.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinho Choi committed Oct 23, 2017
1 parent 6eb204e commit b2f4127
Show file tree
Hide file tree
Showing 36 changed files with 2,695 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/dynamic/test/HanoiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import dynamic.hanoi.AbstractHanoi;
import dynamic.hanoi.DHanoi;
import dynamic.hanoi.RHanoi;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -46,8 +45,7 @@ public void compare()
assertEquals(recursive.solve(k, source, intermediate, destination), dynamic.solve(k, source, intermediate, destination));
}

@Test
@Ignore
// @Test
public void testSpeed()
{
final int ITERATIONS = 100;
Expand Down
97 changes: 97 additions & 0 deletions src/graph/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright 2014, Emory University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package graph;



/**
* @author Jinho D. Choi ({@code [email protected]})
*/
public class Edge implements Comparable<Edge>
{
private int i_source;
private int i_target;
private double d_weight;

public Edge(int source, int target, double weight)
{
init(source, target, weight);
}

public Edge(Edge edge)
{
init(edge.getSource(), edge.getTarget(), edge.getWeight());
}

private void init(int source, int target, double weight)
{
setSource(source);
setTarget(target);
setWeight(weight);
}

// ============================== Getter ==============================
public int getSource()
{
return i_source;
}

public int getTarget()
{
return i_target;
}

public double getWeight()
{
return d_weight;
}

// ============================== Setter ==============================
public void setSource(int vertex)
{
i_source = vertex;
}

public void setTarget(int vertex)
{
i_target = vertex;
}

public void setWeight(double weight)
{
d_weight = weight;
}

public void addWeight(double weight)
{
d_weight += weight;
}

// ===================================================================
@Override
public int compareTo(Edge edge)
{
double diff = d_weight - edge.d_weight;
if (diff > 0) return 1;
else if (diff < 0) return -1;
else return 0;
}

public String toString()
{
return String.format("%d <- %d : %f", getTarget(), getSource(), getWeight());
}
}
162 changes: 162 additions & 0 deletions src/graph/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* Copyright 2014, Emory University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package graph;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import utils.Utils;

/**
* @author Jinho D. Choi ({@code [email protected]})
*/
public class Graph
{
private List<Edge>[] e_incoming;

@SuppressWarnings("unchecked")
public Graph(int size)
{
e_incoming = (List<Edge>[])Utils.createEmptyListArray(size);
}

// ============================== Getter ==============================
public List<Edge> getIncomingEdges(int target)
{
return e_incoming[target];
}

public List<Edge> getAllEdges()
{
List<Edge> edges = new ArrayList<>();

for (int i=0; i<size(); i++)
edges.addAll(e_incoming[i]);

return edges;
}

public int size()
{
return e_incoming.length;
}

@SuppressWarnings("unchecked")
public Deque<Edge>[] getOutgoingEdges()
{
Deque<Edge>[] edges = (Deque<Edge>[])Utils.createEmptyDequeArray(size());

for (int target=0; target<size(); target++)
{
for (Edge edge : getIncomingEdges(target))
edges[edge.getSource()].add(edge);
}

return edges;
}

public Deque<Integer> getVerticesWithNoIncomingEdges()
{
Deque<Integer> deque = new ArrayDeque<>();
int i, size = size();

for (i=0; i<size; i++)
{
if (getIncomingEdges(i).isEmpty())
deque.add(i);
}

return deque;
}

// ============================== Setter ==============================
public Edge setDirectedEdge(int source, int target, double weight)
{
List<Edge> edges = getIncomingEdges(target);
Edge edge = new Edge(source, target, weight);
edges.add(edge);
return edge;
}

public void setUndirectedEdge(int source, int target, double weight)
{
setDirectedEdge(source, target, weight);
setDirectedEdge(target, source, weight);
}

// ============================== Checks ==============================
public boolean containsCycle()
{
Deque<Integer> notVisited = new ArrayDeque<>();
for (int i=0; i<size(); i++) notVisited.add(i);

while (!notVisited.isEmpty())
{
if (containsCycleAux(notVisited.poll(), notVisited, new HashSet<>()))
return true;
}

return false;
}

private boolean containsCycleAux(int target, Deque<Integer> notVisited, Set<Integer> visited)
{
notVisited.remove(target);
visited.add(target);

for (Edge edge : getIncomingEdges(target))
{
if (visited.contains(edge.getSource()))
return true;

if (containsCycleAux(edge.getSource(), notVisited, new HashSet<>(visited)))
return true;
}

return false;
}

public boolean isEmpty()
{
for (int i=0; i<size(); i++)
{
if (!getIncomingEdges(i).isEmpty())
return false;
}

return true;
}

// ===================================================================
public String toString()
{
StringBuilder build = new StringBuilder();

for (int i=0; i<e_incoming.length; i++)
{
build.append(i);
build.append(" <- ");
build.append(e_incoming[i].toString());
build.append("\n");
}

return build.toString();
}
}
67 changes: 67 additions & 0 deletions src/graph/Subgraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014, Emory University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package graph;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Jinho D. Choi ({@code [email protected]})
*/
public class Subgraph
{
private List<Edge> l_edges;
private Set<Integer> s_vertices;

public Subgraph()
{
l_edges = new ArrayList<>();
s_vertices = new HashSet<>();
}

public Subgraph(Subgraph graph)
{
l_edges = new ArrayList<>(graph.getEdges());
s_vertices = new HashSet<>(graph.getVertices());
}

// ============================== Getter ==============================
public List<Edge> getEdges()
{
return l_edges;
}

public Set<Integer> getVertices()
{
return s_vertices;
}

// ============================== Setter ==============================
public void addEdge(Edge edge)
{
l_edges.add(edge);
s_vertices.add(edge.getSource());
s_vertices.add(edge.getTarget());
}

// ============================== Checks ==============================
public boolean contains(int vertex)
{
return s_vertices.contains(vertex);
}
}
Loading

0 comments on commit b2f4127

Please sign in to comment.