-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jinho Choi
committed
Oct 23, 2017
1 parent
6eb204e
commit b2f4127
Showing
36 changed files
with
2,695 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.