Monday, August 18, 2014

Graph.java

Check here, for Graph basics. Below is adjacency list based graph implementation in Java.

package graph.algo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

/**
 * Graph object which maps source and destination vertex. It's adjacency list
 * implementation. It maps vertex to node. Node encapsulates a vertex and the
 * weight.
 * 
 * It is represented as : vertexSource ---------> Node
 * 
 * So in above relationship- the weight is put in Node class. It is implicitly
 * same as putting weight on the edge. And to allow more than one Node
 * connection; map stores the list of Nodes.
 * 
 * Set<V> vertices : for fast retrieval instead of traversing the graph and
 * getting the list of vertices.
 * 
 * Can be used to model : 1) Directed graph 2) Undirected graph : make sure to
 * call addEdge(src,dest, wt) and addEdge(dest, src, wt) 3) Weighted and
 * Unweighted : make weight as 0/default value for Unweighted graph
 * 
 * Required Java Version = 7
 * 
 * @author Siddheshwar
 * 
 * @param <V>
 */
public class Graph<V> {
 private Map<V, List<Node<V>>> adjacencyList;
 private Set<V> vertices;
 private static final int DEFAULT_WEIGHT = Integer.MAX_VALUE;

 public Graph() {
  this.adjacencyList = new HashMap<>();
  vertices = new HashSet<>();
 }

 /**
  * Node class to represent nodes in the graph. The weight attribute allows
  * you to represent weighted graph.
  * 
  * @param <V>
  */
 private static class Node<V> {
  private V name; // Vertex name
  private int weight;

  public Node(V name, int weight) {
   this.name = name;
   this.weight = weight;
  }

  public V getName() {
   return name;
  }

  public int getWeight() {
   return weight;
  }

  @Override
  public int hashCode() {
   return this.getName().hashCode();
  }

  @Override
  public String toString() {
   return "(" + this.weight + ")" + this.name;
  }
 }

 /**
  * Getter method for graph object
  * 
  * @return Map<V, List<Node<V>>>
  */
 public Map<V, List<Node<V>>> getGraph() {
  return this.adjacencyList;
 }

 /**
  * Checks if graph is empty ? Even if there is a single vertex; it won't be
  * considered as empty.
  * 
  * @return
  */
 public boolean isEmpty() {
  return this.vertices.isEmpty();
 }

 /**
  * Add a new neighbor node or adjacent node for the given source vertex.
  * Vertex to node connection forms Edge.
  * 
  * @param src
  * @param destNode
  */
 private void addEdge(V src, Node<V> destNode) {
  List<Node<V>> adjacentVertices = adjacencyList.get(src);
  if (adjacentVertices == null || adjacentVertices.isEmpty()) {
   adjacentVertices = new ArrayList<Node<V>>();
   adjacentVertices.add(destNode);
  } else {
   adjacentVertices.add(destNode);
  }
  adjacencyList.put(src, adjacentVertices);
 }

 /**
  * Add an edge between src and destination vertices
  * 
  * @param src
  * @param dest
  * @param weight
  */
 public void addEdge(V src, V dest, int weight) {
  Objects.requireNonNull(src);
  Objects.requireNonNull(dest);

  this.addEdge(src, new Node<>(dest, weight));

  // update vertices set
  this.vertices.add(src);
  this.vertices.add(dest);
 }

 /**
  * Add an edge between src and destination vertices overloaded method to
  * create Unweighted graph
  * 
  * @param src
  * @param dest
  * @param weight
  */
 public void addEdge(V src, V dest) {
  Objects.requireNonNull(src);
  Objects.requireNonNull(dest);

  this.addEdge(src, new Node<>(dest, DEFAULT_WEIGHT));

  // update vertices set
  this.vertices.add(src);
  this.vertices.add(dest);
 }

 /**
  * Takes two vertices and checks if there is a path between v1 and v2.
  * Doesn't take vice-versa.
  * 
  * @param source
  * @param destination
  * @return
  */
 public boolean hasRelationship(V source, V destination) {
  if (source == null && destination == null)
   return true;
  if (source != null && destination == null)
   return true;
  if (source == null && destination != null)
   return false;

  List<Node<V>> nodes = null;

  if (adjacencyList.containsKey(source)) {
   nodes = adjacencyList.get(source);
   if (nodes != null && !nodes.isEmpty()) {
    for (Node<V> neighbors : nodes) {
     if (neighbors.getName().equals(destination))
      return true;
    }
   }
  }
  return false;
 }

 /**
  * get weight of the edge
  * 
  * @param src
  * @param dest
  * @return weight
  */
 public int getWeight(V src, V dest) {
  int weight = DEFAULT_WEIGHT;
  if (this.hasRelationship(src, dest)) {
   List<Node<V>> adjacentNodes = this.adjacencyList.get(src);
   for (Node<V> node : adjacentNodes) {
    if (node.getName().equals(dest)) {
     weight = node.getWeight();
    }
   }
  }
  return weight;
 }

 /**
  * Get adjacent vertices of the given vertex. This method returns all edges
  * starting from the given vertex. You can call this method iteratively on
  * all vertices to get all edges.
  * 
  * @param vertex
  *            source vertex
  * @return List<V> list of adjacent vertices
  */
 public List<V> getAdjacentVertices(V vertex) {
  List<Node<V>> adjacentNodes = this.adjacencyList.get(vertex);
  List<V> neighborVertex = new ArrayList<>();

  if ((adjacentNodes != null) && !adjacentNodes.isEmpty()) {
   for (Node<V> v : adjacentNodes) {
    neighborVertex.add(v.getName());
   }
  }
  return neighborVertex;
 }

 /**
  * Returns the unmodifiable collection of all vertices of the graph
  * 
  * @return Set<V>
  */
 public Set<V> getAllVertices() {
  return Collections.unmodifiableSet(this.vertices);
 }

 /**
  * Remove vertex and it's relationship from the graph. Also remove vertex
  * from the vertices set
  * 
  * 
  * 1---->2---->3 removeVertex(2) will leave graph as 1 3 It will remove 2 as
  * well as it's relationship
  * 
  * @param vertex
  */
 public boolean removeVertex(V vertex) {
  Objects.requireNonNull(vertex);

  if (!this.vertices.contains(vertex))
   return false;

  Iterator<Entry<V, List<Node<V>>>> itr = this.adjacencyList.entrySet()
    .iterator();

  while (itr.hasNext()) {
   Entry<V, List<Node<V>>> e = itr.next();
   List<Node<V>> vs = e.getValue();
   // remove vertex if it's found as a key in map
   if (vertex.equals(e.getKey())) {
    itr.remove();
   }

   Iterator<Node<V>> listIterator = vs.iterator();
   while (listIterator.hasNext()) {
    Node<V> ver = listIterator.next();
    // remove vertex if it's found as a value in mapped list
    if (vertex.equals(ver.getName())) {
     listIterator.remove();
    }
   }
  }

  // remove vertex from vertices collection
  Iterator<V> itrVertices = this.vertices.iterator();
  while (itrVertices.hasNext()) {
   if (vertex.equals(itrVertices.next())) {
    itrVertices.remove();
    break;
   }
  }
  return true;
 }

 /**
  * Returns graph as a string returns set of edges and vertices
  */
 public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append("Set of Edges :\n");
  for (V v : this.adjacencyList.keySet()) {
   List<Node<V>> neighbour = this.adjacencyList.get(v);
   for (Node<V> vertex : neighbour) {
    if (vertex.getWeight() != DEFAULT_WEIGHT) {
     sb.append(v + " -- (" + vertex.getWeight() + ")--->"
       + vertex.getName() + "\n");
    } else {
     sb.append(v + " ------->" + vertex.getName() + "\n");
    }

   }
  }
  sb.append("& Set of vertices :" + this.getAllVertices());
  return sb.toString();
 }

 /**
  * Method to unit test
  */
 public static void main(String[] args) {
  Graph<String> graph = new Graph<String>();
  graph.addEdge("BLR", "SFO", 100);
  graph.addEdge("BLR", "HK", 50);
  graph.addEdge("BLR", "LA", 70);
  graph.addEdge("LA", "SFO", 20);
  graph.addEdge("HK", "LA", 60);

  System.out.println(graph.toString());

  System.out.println("\n Path between BLR and LA exists ? :"
    + graph.hasRelationship("BLR", ""));

  System.out.println(" Remove vertex BLR ? " + graph.removeVertex("LA"));
  System.out.println(graph.toString());
 }
}


Output:
Set of Edges :
HK -- (60)--->LA
LA -- (20)--->SFO
BLR -- (100)--->SFO
BLR -- (50)--->HK
BLR -- (70)--->LA
& Set of vertices :[HK, LA, BLR, SFO]

 Path between BLR and LA exists ? :false
 Remove vertex BLR ? true
Set of Edges :
BLR -- (100)--->SFO
BLR -- (50)--->HK

& Set of vertices :[HK, BLR, SFO]

Saturday, August 9, 2014

Graph Depth-First traversal

Previous post (bfs), I covered BFS traversal. This post, I will be focusing on Depth-First traversal. 

The difference between BFS and DFS traversal lies in the order in which vertices are explored. And this mainly comes due to data structure used to do traversal. BFS uses queue whereas DFS uses stack data structure to perform traversal. Below diagram illustrates the order of traversal. 


DFS Progress

DFS Implementation

DFS implementation uses Graph.java from the post. Below class has two implementations of DFS traversals (recursive and iterative). Method, traverse(..) is stack based implementation whereas traverseRecur(..) is recursive implementation.

package graph.algo;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Depth-First Traversal of a graph represented as adjacency list
 * 
 * @author Siddheshwar
 * 
 */
public class DFS<V> {
 private Graph<V> graph; // graph on which DFS will be performed
 private Deque<V> stack; // Deque used to implement stack
 private Set<V> visited; // stores set of visited nodes

 public DFS(Graph<V> graph) {
  this.graph = graph;
  stack = new ArrayDeque<>();
  visited = new LinkedHashSet<>(); // to maintain the insertion order
 }

 /**
  * Iterative/stack based DFS implementation
  * 
  * @param source
  */
 public void traverse(V source) {
  Objects.requireNonNull(source, "source is manadatory!");
  if (this.graph == null || this.graph.isEmpty()) {
   throw new IllegalStateException(
     "Valid graph object is required !!!");
  }

  stack.push(source);
  this.markAsVisited(source);
  boolean pop = false;
  V stackTopVertex;
  System.out.print("  " + source);

  while (!stack.isEmpty()) {

   if (pop == true)
    stackTopVertex = stack.pop();
   else
    stackTopVertex = stack.peek();

   List<V> neighbors = graph.getAdjacentVertices(stackTopVertex);

   if (!neighbors.isEmpty()
     && hasUnvisitedNeighbor(neighbors, visited)) {
    for (V a : neighbors) {
     if (!this.isVertexVisited(a)) {
      System.out.print("  " + a);
      visited.add(a);
      stack.push(a);
      // break from loop if an unvisited neighbor is found
      break;
     }
    }
   } else {
    // if all neighbors are visited
    pop = true;
   }
  }
 }

 /**
  * Recursive implementation of DFS
  * 
  * @param source
  */
 public void traverseRecur(V source) {
  Objects.requireNonNull(source, "source is manadatory!");

  this.markAsVisited(source);
  System.out.print(" " + source);

  // get neighbors in sorted manner
  List<V> neighbors = this.graph.getAdjacentVertices(source);

  for (V n : neighbors) {
   if (!this.isVertexVisited(n)) {
    traverseRecur(n);
   }
  }
 }

 /**
  * checks if any of the neighbor is unvisited
  * 
  * @param neighbors
  * @param visited
  * @return
  */
 private boolean hasUnvisitedNeighbor(List<V> neighbors, Set<V> visited) {
  for (V i : neighbors) {
   if (!visited.contains(i))
    return true;
  }
  return false;
 }

 /**
  * Returns true if vertex is already visited
  * 
  * @param i
  * @return
  */
 private boolean isVertexVisited(V i) {
  return this.visited.contains(i);
 }

 /**
  * Mark a vertex visited
  * 
  * @param i
  */
 private void markAsVisited(V i) {
  this.visited.add(i);
 }

 // test method
 public static void main(String[] args) {
  Graph<Integer> graph = new Graph<>();
  graph.addEdge(1, 2);
  graph.addEdge(1, 5);
  graph.addEdge(1, 6);
  graph.addEdge(2, 3);
  graph.addEdge(2, 5);
  graph.addEdge(3, 4);
  graph.addEdge(5, 4);

  // for undirected graph
  graph.addEdge(2, 1);
  graph.addEdge(5, 1);
  graph.addEdge(6, 1);
  graph.addEdge(3, 2);
  graph.addEdge(5, 2);
  graph.addEdge(4, 3);
  graph.addEdge(4, 5);

  System.out.print("DFS -->");
  DFS<Integer> dfs = new DFS<>(graph);

  /**
   * stack based DFS traversal
   */
  dfs.traverse(1);

  /**
   * Recursive DFS traversal
   */
  // dfs.traverseRecur(1);

  // validation
  /**
   * after traversal; stack should be empty. And visited should have
   * vertices in DFS order
   */
  System.out.println("\nIs Stack is empty :"
    + (dfs.stack.isEmpty() ? "yes" : "no"));
  System.out.println("visited :" + dfs.visited);

 }

}

Output:
DFS -->  1  2  3  4  5  6
Is Stack is empty :yes
visited :[1, 2, 3, 4, 5, 6]



Time Complexity

Assuming above implementation of Graph i.e. Adjacency List, |V| is number of vertices and |E| is number of edges. 
So complexity is O(|V|+|E|)

--
happy learning !!!