Pages

Saturday, 3 May 2014

Tuple Implementation and Conversion to Adjacency List Graph in Java

In this tutorial, first we will discuss about a simple implementation of Tuple in Java and then work on conversion of a list of tuples to Adjacency List Graph. Please click on this link to read the implementation of Adjacency List Graph in Java that we discussed earlier.

A tuple is an ordered list of elements. We can group together pairs of values by surrounding with parentheses like:
year_born = ("Someone", 1980)
This is an example of a data structure (a mechanism for grouping and organizing data to make it easier to use).

Following source code represents a very simple structure for tuple and it assumes a tuple consists of two entries.


 public class Tuple<X> {  
      private X t1;  
      private X t2;  
      public Tuple(X t1, X t2) {  
           this.t1 = t1;  
           this.t2 = t2;  
      }  
      public X getT1() {  
           return t1;  
      }  
      public void setT1(X t1) {  
           this.t1 = t1;  
      }  
      public X getT2() {  
           return t2;  
      }  
      public void setT2(X t2) {  
           this.t2 = t2;  
      }  
 }  

As an example consider this question: Dynamically build a graph from a stream of father-child tuples.

We use this Tuple structure to represent father-child tuples. Then we write a method that accepts list of tuples and convert it to Adjacency List Graph that we discussed earlier (here).

Following shows the source code for Tuple to Graph conversion:
 import uk.ac.soton.ecs.datastructure.graph.adjacencyList.AdjacencyListGraph;  
 import uk.ac.soton.ecs.datastructure.graph.adjacencyList.Vertix;  
 public class TupleToGraph<X> {  
      public AdjacencyListGraph<X> convertTupleToAdjacencyListGraph(  
                List<Tuple<X>> tuples) {  
           AdjacencyListGraph<X> graph = new AdjacencyListGraph<X>();  
           if (tuples != null && !tuples.isEmpty()) {  
                for (Tuple<X> tuple : tuples) {  
                     Vertix<X> v1 = graph.getVertex(tuple.getT1());  
                     Vertix<X> v2 = graph.getVertex(tuple.getT2());  
                     if (v1 == null) {  
                          v1 = graph.addVertex(tuple.getT1());  
                     }  
                     if (v2 == null) {  
                          v2 = graph.addVertex(tuple.getT2());  
                     }  
                     graph.addEdge(v2, v1);  
                }  
           }  
           return graph;  
      }  
 }  
And following shows a simple JUnit to test this method:
 import java.util.ArrayList;  
 import java.util.List;  
 import org.junit.Test;  
 import uk.ac.soton.ecs.datastructure.graph.adjacencyList.AdjacencyListGraph;  
 public class TupleToGraphTest {  
      @Test  
      public void testConvertTupleToAdjacencyListGraph() {  
           System.out.println("testConvertTupleToAdjacencyListGraph");  
           List<Tuple<String>> tuples = new ArrayList<Tuple<String>>();  
           tuples.add(new Tuple<String>("v1", "v2"));  
           tuples.add(new Tuple<String>("v1", "v3"));  
           tuples.add(new Tuple<String>("v1", "v4"));  
           tuples.add(new Tuple<String>("v3", "v5"));  
           tuples.add(new Tuple<String>("v3", "v6"));  
           tuples.add(new Tuple<String>("v4", "v7"));  
           tuples.add(new Tuple<String>("v4", "v8"));  
           TupleToGraph<String> tupleToGraph = new TupleToGraph<String>();  
           AdjacencyListGraph<String> graph = tupleToGraph  
                     .convertTupleToAdjacencyListGraph(tuples);  
           System.out.println(graph.toString());  
           System.out.println("***********************************************");  
      }  
 }  

References

Tuples on Open Book Project

No comments:

Post a Comment