Skip to content

Commit

Permalink
Adding comments for EdgeType num
Browse files Browse the repository at this point in the history
  • Loading branch information
anuchandy committed Jul 25, 2016
1 parent 0493b5e commit afc4173
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ public void visitNode(U node) {
}

@Override
public void visitEdge(String fromKey, String toKey, GraphEdgeType edgeType) {
System.out.println("{" + fromKey + ", " + toKey + "} " + edgeType);
if (edgeType == GraphEdgeType.BACK) {
public void visitEdge(String fromKey, String toKey, EdgeType edgeType) {
if (edgeType == EdgeType.BACK) {
throw new IllegalStateException("Detected circular dependency: " + findPath(fromKey, toKey));
}
}
Expand Down
90 changes: 51 additions & 39 deletions azure-client-runtime/src/main/java/com/microsoft/azure/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
import java.util.Map;
import java.util.Set;

/**
* The edge types in a graph.
*/
enum GraphEdgeType {
TREE,
FORWARD,
BACK,
CROSS
}

/**
* Type representing a directed graph data structure.
* <p>
Expand Down Expand Up @@ -61,30 +51,6 @@ public void addNode(U node) {
graph.put(node.key(), node);
}

/**
* Represents a visitor to be implemented by the consumer who want to visit the
* graph's nodes in DFS order.
*
* @param <U> the type of the node
*/
interface Visitor<U> {
/**
* visit a node.
*
* @param node the node to visited
*/
void visitNode(U node);

/**
* visit an edge.
*
* @param fromKey key of the from node
* @param toKey key of the to node
* @param graphEdgeType the edge type
*/
void visitEdge(String fromKey, String toKey, GraphEdgeType graphEdgeType);
}

/**
* Perform DFS visit in this graph.
* <p>
Expand Down Expand Up @@ -128,22 +94,22 @@ private void dfs(Visitor visitor, Node<T> node) {
processed.add(fromKey);
}

private GraphEdgeType edgeType(String fromKey, String toKey) {
private EdgeType edgeType(String fromKey, String toKey) {
if (parent.containsKey(toKey) && parent.get(toKey).equals(fromKey)) {
return GraphEdgeType.TREE;
return EdgeType.TREE;
}

if (visited.contains(toKey) && !processed.contains(toKey)) {
return GraphEdgeType.BACK;
return EdgeType.BACK;
}

if (processed.contains(toKey) && entryTime.containsKey(toKey) && entryTime.containsKey(fromKey)) {
if (entryTime.get(toKey) > entryTime.get(fromKey)) {
return GraphEdgeType.FORWARD;
return EdgeType.FORWARD;
}

if (entryTime.get(toKey) < entryTime.get(fromKey)) {
return GraphEdgeType.CROSS;
return EdgeType.CROSS;
}
}

Expand All @@ -157,4 +123,50 @@ protected String findPath(String start, String end) {
return findPath(start, parent.get(end)) + " -> " + end;
}
}

/**
* The edge types in a graph.
*/
enum EdgeType {
/**
* An edge (u, v) is a tree edge if v is visited the first time.
*/
TREE,
/**
* An edge (u, v) is a forward edge if v is descendant of u.
*/
FORWARD,
/**
* An edge (u, v) is a back edge if v is ancestor of u.
*/
BACK,
/**
* An edge (u, v) is a cross edge if v is neither ancestor or descendant of u.
*/
CROSS
}

/**
* Represents a visitor to be implemented by the consumer who want to visit the
* graph's nodes in DFS order by calling visit method.
*
* @param <U> the type of the node
*/
interface Visitor<U> {
/**
* visit a node.
*
* @param node the node to visited
*/
void visitNode(U node);

/**
* visit an edge.
*
* @param fromKey key of the from node
* @param toKey key of the to node
* @param edgeType the edge type
*/
void visitEdge(String fromKey, String toKey, EdgeType edgeType);
}
}

0 comments on commit afc4173

Please sign in to comment.