AdSense

Wednesday, August 26, 2015

Scala note 1: Pascal’s Triangle, Parentheses Balancing and Counting Change

I decided to take a look on Scala, so that next time I read a source code, at least I know what I am reading.

I choose Coursera's Functional Programming Principles in Scala as my reference (I do wish they open the class again, however, the same time when they open the class last year, I was fighting with Java ðŸ˜ž.)

First week's class are basics. Two concepts need to be remember:

Call by value: evaluates function arguments before calling the functionCall by name: evaluates the function first, and then evaluate the arguments if needed
Scala usually do call by value, but we can also do call by name by adding x: => Type to the parameter. 
All three exercise problems are about recursions. The algorithms are easy since those are problems we see all the time. 

Pascal's Triangle
The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a function that computes the elements of Pascal’s triangle by means of a recursive process.Do this exercise by implementing the pascal function in Main.scala, which takes a column c and a row r, counting from 0 and returns the number at that spot in the triangle. For example, pascal(0,2)=1pascal(1,2)=2 and pascal(1,3)=3.def pascal(c: Int, r: Int): Int

def pascal(c: Int, r: Int): Int = {
    if  (c == 0 || c == r || r == 0)  1
    else
      pascal(c - 1, r - 1) + pascal(c, r - 1)
  }

Sort of short...


Parentheses Balancing
Write a recursive function which verifies the balancing of parentheses in a string, which we represent as a List[Char] not a String. For example, the function should return true for the following strings:
  • (if (zero? x) max (/ 1 x))
  • I told him (that it’s not (yet) done). (But he wasn’t listening)
The function should return false for the following strings:
  • :-)
  • ())(
The last example shows that it’s not enough to verify that a string contains the same number of opening and closing parentheses.Do this exercise by implementing the balance function in Main.scala. Its signature is as follows:def balance(chars: List[Char]): Boolean
def balance(chars: List[Char]): Boolean = {
    def balance(chars: List[Char], left: Int): Boolean ={
      if(chars.isEmpty) left == 0
      else
        if (chars.head == ')') {left > 0 && balance(chars.tail, left - 1)}
        else if (chars.head == '(') {balance(chars.tail, left + 1)}
        else {balance(chars.tail, left)} 
    }
    balance(chars, 0)
  }

Basically, we count the left parenthesis ("(") . Each time there is a "(", we increment left. If there is a ")" and if left >0, then it is imbalanced, otherwise decrease left and check the substring. 

Counting Change
Write a recursive function that counts how many different ways you can make change for an amount, given a list of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomiation 1 and 2: 1+1+1+1, 1+1+2, 2+2.Do this exercise by implementing the countChange function in Main.scala. This function takes an amount to change, and a list of unique denominations for the coins. Its signature is as follows:def countChange(money: Int, coins: List[Int]): Int


def countChange(money: Int, coins: List[Int]): Int = {
    def countChange(money:Int, coins: List[Int], count:Int): Int = {
      if (money < 0) count
      else
        if (coins.isEmpty) 
          if (money == 0) count + 1 else count
        else
          countChange(money - coins.head, coins, count) + countChange(money, coins.tail, count)
    }
    countChange(money, coins, 0)
  }

To count all possibilities, we need to iterate all denominations in coins. Using recursion, it is the count of using coins[0] plus the count of not using coins[0], and recursively check all the denominations.


Source on git

Sunday, August 16, 2015

Apache Kafka Java API example

I've just started learning Apache Kafka, and I realize there are not much documentation and examples on the project. So I decide to prepare my own notes.

Start Kafka
Kafka can be installed through source or compiled package. If you are interested in doing it via source, see this git. 

To make things easier, I choose the compiled package. Since everything is compiled, all we need to do is to start the servers. 
Kafka runs on ZooKeeper, so the first thing is to start the ZooKeeper server:
In the directory where Kafka is installed:

bin/zookeeper-server-start.sh config/zookeeper.properties

In a different window/tab, start Kafka server:

bin/kafka-server-start.sh config/server.properties

Create a topic
A topic is a category/feed name to which message are published. The producer sends message to the topic it chooses (see the following code for details), and the consumer accepts messages to the topic it wants.
In a different window/tab, create a topic (named "test"):

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

Create producer and consumers through Java API
If you just want to get your feet wet, follow the doc and try creating producers and consumers through command line.

Here I use Eclipse to make things easier.

First, in Eclipse, create a Maven project (refer this post for creating a Maven project in Eclipse). To include dependencies in pom.xml, open pom.xml, at the bottom click pom.xml (see the figure below), and add the following dependencies:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Kafka</groupId>
  <artifactId>KafkaProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>kafkatest</name>
  <url>http://maven.apache.org</url>
  <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies>
   <dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.10</artifactId>
    <version>0.8.2.1</version>
    <scope>compile</scope>
    <exclusions> 
     <exclusion> 
      <artifactId>jmxri</artifactId> 
      <groupId>com.sun.jmx</groupId> 
     </exclusion> 
     <exclusion> 
      <artifactId>jms</artifactId> 
      <groupId>javax.jms</groupId> 
     </exclusion> 
     <exclusion> 
      <artifactId>jmxtools</artifactId> 
      <groupId>com.sun.jdmk</groupId> 
     </exclusion> 
    </exclusions>
   </dependency>
  </dependencies>
</project>


Be sure to include all exclusions, otherwise you will get error.

Before writing our Producer class, start a consumer in a new window/tab in terminal (in the directory where Kafka is installed):

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

Now create a class ProducerTest (or whatever name you want).

import java.util.*;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class ProducerTest {
 public static void main(String[] args) { 
  Properties props = new Properties();
  props.put("zk.connect", "localhost:2181"); 
  props.put("serializer.class","kafka.serializer.StringEncoder");
  props.put("metadata.broker.list", "localhost:9092");
  ProducerConfig config = new ProducerConfig(props);
  Producer<string, string> producer = new Producer<string, string>(config);
  for (long nEvents = 0; nEvents < 10; nEvents++){
   System.out.println("Creating events " + nEvents);
   long runtime = new Date().getTime();
   String msg = runtime + ", Shirley is awesome.";
   producer.send(new KeyedMessage<string, string>("test", msg));
  } 
 }
}

Now if you run the code, you will see messages showing up in the window/tab where consumer is created.

syang:kafka_2.10-0.8.2.0 syang$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
test
1439686893619, Shirley is awesome.
1439686893722, Shirley is awesome.
1439686893724, Shirley is awesome.
1439686893725, Shirley is awesome.
1439686893727, Shirley is awesome.
1439686893728, Shirley is awesome.
1439686893729, Shirley is awesome.
1439686893731, Shirley is awesome.
1439686893732, Shirley is awesome.
1439686893733, Shirley is awesome.


Start a producer in a new window/tab:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

Create a ConsumerTest class:
The consumer class is more complicated since it involves multithreading.

public class ConsumerTest extends Thread{
 private final ConsumerConnector consumer;
 private final String topic;
 
 public ConsumerTest(String topic){
  consumer = kafka.consumer.Consumer
    .createJavaConsumerConnector(createConsumerConfig());
  this.topic = topic;
 }
 public static ConsumerConfig createConsumerConfig(){
  Properties props = new Properties();
  props.put("zookeeper.connect", "localhost:2181");
  props.put("group.id", "test_group");
  props.put("zookeeper.session.timeout.ms", "400000");
  props.put("zookeeper.sync.time.ms", "200");
  props.put("auto.commit.interval.ms", "1000");
  return new ConsumerConfig(props);
 }
 
 public void run(){
  Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
  topicCountMap.put(topic, 1);
  Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
  KafkaStream<byte[],byte[]> stream = consumerMap.get(topic).get(0);
  ConsumerIterator<byte[], byte[]> it = stream.iterator();
  while (it.hasNext())
   System.out.println(new String(it.next().message()));
 } 
 public static void main(String[] args) {
  ConsumerTest consumerThread = new ConsumerTest("test");
  consumerThread.start();
 }
}


Configurations
Both Producer and Consumer class requires a Properties object (implemented as a HashTable), which set the configurations for Producer and Consumer.

Producer configurations:
zk.connect: Specifies the Zookeeper connection string in the form of hostname:port/chroot. The chroot is a base directory which is prepended to all path operations. This namespaces all Kafka znodes to allow sharing with other applications on the same zookeeper cluster.
serializer.class: Used to encode data of type T into a Kafka message.
metadata.broker.list:the producer will only use it for getting metadata(topics, partitions and replicas). The socket connection for sending the actual data will be established based on the broker information returned in the metadata. Format host1:port1, host2, port2.

Consumer configurations:
group.id: A string that uniquely identifies the group of consumer processes to which this consumer belongs(producer sends messages to all consumer groups, each consumer receive desired messages).
zookeeper.session.timeout.ms: If the consumer fails to heartbeat to ZooKeeper for this period of time it is considered dead and a rebalance will occur.
zookeeper.sync.tims.ms: How far a zookeeper follower can be behind a zookeeper lead.
auto.commit.interval.ms: the frequency in ms that the consumer offsets are committed to a zookeeper.

 References:
[1] Kafka documentation
[2] http://vulab.com/blog/?p=611
[3] http://vulab.com/blog/?p=623
[4] http://blog.csdn.net/ganglia/article/details/12651891

Saturday, August 15, 2015

Merge K sorted linked lists/arrays revisited

K sorted arrays

I got a comment on my previous post Merge K sorted arrays, and I realized that there are much easier (and shorter) solutions to solve this problem. Here is the code snippet in both Java and Python.

Java:

public static List<Integer> merged (List<int[]> arrays){
  List<Integer> mergedArrays = new ArrayList<Integer> ();
  if (arrays == null || arrays.size() == 0){
   return mergedArrays;
  }
  for (int[] array : arrays){
   for (int i : array)
    mergedArrays.add(i);
  }
  Collections.sort(mergedArrays);
  return mergedArrays;
 }

Python:


def merged(lists):
    if lists is None or len(lists) == 0:
        return None
    mergedArray = [n for array in lists for n in array]
    return sorted(mergedArray)

Yeah, you can't disagree that the Pythonic way sometimes make things too easy to believe.

Why does this work:
Since in any case, we have to traverse the whole every element in the list of arrays, the complexity is O (mn), where m is the length of the list and n is the average length of each array.  Collections.sort() implements Timsort (see here, here and here) which on average has complexity O(nlogn). The time complexity is the same as using a priority queue, but with much shorter code.


K sorted lists

This gives me a reason to rethink the merge K sorted linked lists problem.
Unfortunately, since we need to return a linked list, we cannot use any built-in sorting methods.
I rewrite the code using Python. In Python, the built-in heapq only provides sorting based on natural order. Since the queue can sort tuples based on its first element, one solution could be wrapping the elements to be sorted to tuples (priority, element). See the code for detail[1].

import heapq
from mergeAndSort.ListNode import ListNode

class PriorityQueue(object):
    def __init__(self, initial = None, key=lambda x:x):
        self.key = key
        if initial:
            self._data = [(key(item), item) for item in initial]
            heapq.heapify(self._data)
        else:
            self._data = []

    def push(self, item):
        heapq.heappush(self._data, (self.key(item), item))

    def pop(self):
        return heapq.heappop(self._data)[1]

    def __len__(self):
        return len(self._data)

    def empty(self):
        return len(self._data) == 0

class ListNode(object):
    def __init__(self, val):
        self.val = val
        self.next = None

def mergeKLists(lists):
    if lists is None or len(lists) == 0:
            return None
    pq = PriorityQueue(key=lambda x:x.val)
    for n in lists:
        if n is not None:
            pq.push(n)
    head = ListNode(-1)
    h = head
    while not pq.empty():
        n = pq.pop()
        head.next = n
        head = head.next
        if n.next is not None:
            pq.push(n.next)
    return h.next

Comparison
Based on Leetcode's compiler, this Python implementation is not faster than the Java one. I'm sure there should be better solutions, please let me know.

Reference:
[1] http://stackoverflow.com/questions/8875706/python-heapq-with-custom-compare-predicate

Saturday, August 8, 2015

Too young to figure out, too old to wait

I finally settled down in CA, got the job I like and started to work on things I want. Yet there is still one thing haunt my mind: I wish I was three years younger. I probably don't have to worry about lots of things, don't have to be so scared about making a wrong decision, and probably don't have to make so many plans that stress me out.

They say it's never too late.

I want to say "Yes, it is", but I decided to change and go with "It depends". Last year, someone told me about the CEO of a startup is three years younger than him and is better than him in both technical skills and persistence. I told him you came from a different background and there is never too late for anything. He thought I didn't understand him and I was naive.

Now I am in a similar situation hoping if I could be three years younger. I definitely don't want what he wants, but as I step into my job, things are getting clearer. However, I have just started as a new grad and I have so many things I need to learn. And life is getting more complicated as I am older. I feel suffocated sometime.

Last night, I had a long conversation with my friend. The third time he talked about an amazing personality test, I told him I wouldn't do it if it will cost me anything. It's not about I don't want to know more about myself. Sometimes I am curious about what about me that is still hidden from me. Yet I am confident that I have known enough about myself that anything I still don't know doesn't worth a penny (or I am just arrogant :) ).

The reason I brought up this is because I realize all the stress, fear, regrets and everything else came from my personality: the ultimate double-edged sword -- responsibility. This is definitely a good type of personality (in most ways), but it becomes a burden when one treats it too important, like me. I always feel responsible to my work, my family and my friends. I would avoid taking charge of things because I think the responsibility would suffocate me. Most of them don't, and I always end up well. But the fear of it haunts me and holds me back.

There is one thing I learned from the popular movie Inside out: Everything has two sides, you just have to find the balance. I guess I am still too young (?) to figure that out.

At the end of last year, I regretted that because of my mistake I lost a friend. In the next couple months, I thought I was trying to make things better, it turned out to have made things even worse. In April, he complained to my friend calling me "having negative EQ". I didn't talk to him for three months. Considering he helped me a lot when I needed most (looking for jobs), I tried to amend with him and talked to him again, only to get the angry mail "I think you are annoying, stupid and hypocritical", that did not only hurt but also made me sick. In the next couple days I kept thinking what I did wrong this time, I couldn't think any. After the grievance and emotion was gone, I realize I was not the only one who made things irreparable. Even now I am still regretful for the mistake I made. And from time to time I would think what if... It probably is because I felt so sorry about what I did and thought about the help he gave me that I was too tolerant on how he treated me. And he took it for granted. Then one day I couldn't take it anymore and things got screwed up.

That was the worst relationship experience I have ever had with a person I think overall is decent and in another case I would admire. But now when I think about it every time it makes me sick. I imagine all the time that I have an eraser, wipe everything in my mind and start all over again. Yet I don't have one, and unless there is a huge accident happen to me (please don't) I probably won't lose that part of memory, at least for a very long time. I know it started from me but if he could be less asshole to piss me off things might not be like this.

Yet I don't have a time machine nor Doraemon doraemon cat doraemon cat.  All I have is a lesson I learned after this mess. It probably is true that I learn something for every mistake I make. But I am too old to not having understood lots of things, and I don't feel I am patient enough to wait.

But God is a bitch. She doesn't give you what you want until you struggle enough and ready to give up. They say you never figure out everything. I hope when I am too old to mess everything up I understand most of the things I want. Or not?


Sunday, August 2, 2015

Graph again (Python and Java, adjacency matrix representation)

I decided to start reviewing data structures and algorithms again, and the first thing I chose is my favorite graph.

There are quite a few ways to represent a graph, the most common two are adjacency matrix and adjacency list. In this post, I use adjacency matrix representation. 

Graph and Adjacency Matrix Representation
A graph (see here for formal definition) contains vertices and edges. Edges are used to connect vertices. Directed graphs have directions (of course!), e.g., edge(a, b) != edge (b, a). Undirected graphs, on the contrary, have no directions, e.g., edge(a, b) = edge(b, a). 

So the question is, how to represent graph in program?

Adjacency matrix representation refers to using a matrix of size (number of vertices * number of vertices) to represent the connectivity, i.e., edges. 

Consider the following graph:

The adjacency matrix representation is as follows:


Each row and column represents a vertex, the cell represents the weight of the edge between them. If there is no edge between two vertices, the weight is 0. In this example, all weights are set to 1, it is possible to personalize the weight, e.g., weight(1, 3) = 2, weight(2, 4) = 0.5. 

Since the graph is undirected, if there is an edge between v1 and  v2, then there is an edge between v2 and v1, so the matrix is symmetric. In case of a directed graph, an edge direct from 1 to 3 leads to a value in matrix[1][3], but that does not indicates there is a value in matrix[3][1], unless there is an edge that directs 3 to 1. 

Traverse the graph
There are two ways to traverse a graph, depth-first search (DFS) and breadth-first search (BFS). DFS goes as deep (yeah...) as it can until all the vertices rooted from one vertex are visited then visit a neighbor vertex. BFS goes as broad as it can until all the neighbors of a given vertex are visited then go to the next level. The following two graphs illustrate the path of DFS and BFS if we start by traversing vertex 3. 




Java code
public class Graph {
 protected int adjacencyMatrix[][];
 protected int size; //number of vertices
 
 public Graph(int size) {
  this.size = size;
  adjacencyMatrix = new int[size][size];
 }
 
 public void addEdge(int v1, int v2) {
  if (validity(v1) == false || validity(v2) == false)
   return;
  if (v1 == v2){
   System.out.printf("Same vertex %d and %d\n", v1, v2);
   return;
  }
  adjacencyMatrix[v1][v2] += 1;
  adjacencyMatrix[v2][v1] += 1;
 }
 
 public void removeEdge(int v1, int v2){
  if (validity(v1) == false || validity(v2) == false)
   return;
  if (adjacencyMatrix[v1][v2] == 0){
   System.out.printf("No edge between %d and %d\n", v1, v2);
  }
  adjacencyMatrix[v1][v2] -= 1;
  adjacencyMatrix[v2][v1] -= 1;
 }
 
 public boolean containsEdge(int v1, int v2){
  if (validity(v1) == false || validity(v2) == false)
   return false;
  return adjacencyMatrix[v1][v2] > 0 ? true : false;
 }
 
 public int getSize(){
  return size;
 }
 
 protected boolean validity(int v){
  if (v >= size || v < 0) {
   System.out.printf("Invalid vertex index %d\n", v);
   return false;
  }
  else
   return true; 
 }
 public void dfs(int startVertex){
  boolean[] visited= new boolean[this.size];
  List<integer> path = new ArrayList<integer> ();
  dfs(path, startVertex, visited);
  System.out.println(getPath(path));
 }
 private void dfs(List<integer> pathList, int v, boolean[] visited){
  if (pathList.size() == this.size)
   return;
  if (!validity(v)|| visited[v]) 
   return;
  visited[v] = true;
  pathList.add(v);
  for (int i = 0; i < this.size; i++){
   if (containsEdge(v, i)){
    dfs(pathList, i, visited);
   }
  }
 }
 public void bfs(int v){
  if (!validity(v)) 
   return;
  boolean[] visited = new boolean[this.size];
  List<integer> path = new ArrayList<integer> (this.size);
  Queue<integer> neighbors = new LinkedList<integer> ();
  neighbors.offer(v);
  while(!neighbors.isEmpty() && (path.size() < this.size)){
   int vertex = neighbors.poll();
   path.add(vertex);
   visited[vertex] = true;
   for (int i = 0; i < this.size; i++){
    if(containsEdge(vertex, i) && !visited[i]) {
     neighbors.offer(i);
    }
   }
  }
  System.out.println(getPath(path));
 }
 public String getPath(List<integer> path){
  String p = "";
  for (int v : path)
   p += String.format("%d -> ", v);
  return p.substring(0, p.length() - 3);
 }
}


Python code:
The easiest way to write a graph using Python is:
To use the SparseGraph class provided by APGL library.

from apgl.graph import SparseGraph
import time
def main():
    graph = SparseGraph(5)
    graph.addEdge(0, 3)
    graph.addEdge(1, 4)
    graph.addEdge(2, 3)
    graph.addEdge(2, 4)
    graph.addEdge(1, 3)

    print(graph.depthFirstSearch(3))
    print(graph.breadthFirstSearch(3))

if __name__ == '__main__':
    start_time = time.time()
    main()
    print("Running time: %f microseconds" % ((time.time() - start_time)*1000))



Where is the fun of writing code?!

Using the same structure as I do in Java:

from collections import deque

class Graph(object):
    def __init__(self, size):
        self.adjacencyMatrix = []
        for i in range(size):
            self.adjacencyMatrix.append([0 for i in range(size)])
        self.size = size

    def addEdge(self, v1, v2):
        if not self.validity(v1) and not self.validity(v2):
            return
        if v1 == v2:
            print("Same vertex %d and %d" % (v1, v2))
        self.adjacencyMatrix[v1][v2] += 1
        self.adjacencyMatrix[v2][v1] += 1

    def removeEdge(self, v1, v2):
        if not self.validity(v1) and not self.validity(v2):
            return
        if self.adjacencyMatrix[v1][v2] == 0:
            print("No edge between %d and %d" % (v1, v2))
            return
        self.adjacencyMatrix[v1][v2] -= 1
        self.adjacencyMatrix[v2][v1] -= 1

    def containsEdge(self, v1, v2):
        if not self.validity(v1) and not self.validity(v2):
            return False
        return True if self.adjacencyMatrix[v1][v2] > 0 else False

    def __len__(self):
        return self.size

    def validity(self, v):
        if v >= self.size or v < 0:
            print("Invalid vertex index %d" % v)
            return False
        else:
            return True

    def dfs(self, startVertex):
        if not self.validity(startVertex):
            return;
        visited = []
        for i in range(self.size):
            visited.append(False)
        path = []
        self._dfsR(path, startVertex, visited)
        print(self.getPath(path))

    def _dfsR(self, path, v, visited):
        if(len(path) == self.size):
            return;
        if visited[v]:
            return;
        visited[v] = True;
        path.append(v);
        for i in range(self.size):
            if self.containsEdge(v, i):
                self._dfsR(path, i, visited)

    def bfs(self, v):
        if v >= self.size or v < 0:
            return
        visited = []
        for i in range(self.size):
            visited.append(False)
        path = []
        neighbors = deque()
        neighbors.append(v)
        while neighbors and len(path) < self.size:
            vertex = neighbors.popleft()
            path.append(vertex)
            visited[vertex] = True
            for i in range(self.size):
                if self.containsEdge(vertex, i) and not visited[i]:
                    neighbors.append(i)
        print(self.getPath(path))

    def getPath(self, path):
        p = ""
        for v in path:
            p += "%d -> " % v
        return p[:len(p) - 3]

Performance
This is always my favorite part. Using the following pieces of test code for Java and Python:

public class GraphTester {
 public static void main(String[] args) {
  final long startTime = System.nanoTime();
  Graph g = new Graph(5);
  g.addEdge(0, 3);
  g.addEdge(1, 4);
  g.addEdge(2, 3);
  g.addEdge(2, 4);
  g.addEdge(1, 3);
  //g.addEdge(1, 5);
  
  g.dfs(3);
  g.bfs(3);
  System.out.println("Running time: " + ((System.nanoTime() - startTime)/1000) + "ms");
 }
}


from graph.Graph import Graph
import time
def main():
    g = Graph(5)
    g.addEdge(0, 3)
    g.addEdge(1, 4)
    g.addEdge(2, 3)
    g.addEdge(2, 4)
    g.addEdge(1, 3)

    g.dfs(3)
    g.bfs(3)

if __name__ == '__main__':
    start_time = time.time()
    main()
    print("Running time: %f microseconds" % ((time.time() - start_time)*1000))

And the result:
Python 0.157 ms vs. Java 4495 ms.
And guess what, using SparseGraph class takes 0.118 ms (feeling good ).