Wednesday, October 9, 2013

Java: generic static method

 

http://stackoverflow.com/a/4409139

You need to move type parameter to the method level to indicate that you have a generic method rather than generic class:

e.g.

static <E> void swapInList(List<E> listToSort, int idxOne, int idxTwo) 
{
    E tmpEle = listToSort.get(idxOne);
    listToSort.set(idxOne, listToSort.get(idxTwo));
    listToSort.set(idxTwo, tmpEle);        
}

Friday, October 4, 2013

Algorithm: need the Grey (or ‘Visiting’) node in DFS?

 

In short, the answer is No. But when we ‘traverse’ to a grey (visiting) node, we can immediately deduce that there is a circle there, simply because we know that in this round of traversal, we started at (i.e. still ‘visiting’) the grey node.

http://cs.stackexchange.com/a/9681 

When doing a DFS, any node is in one of three states - before being visited, during recursively visiting its descendants, and after all its descendants have been visited (returning to its parent, i.e., wrap-up phase). The three colors correspond to each of the three states. One of the reasons for mentioning colors and time of visit and return is to explicitly make these distinctions for better understanding.

Of course, there are actual uses of these colors. Consider a directed graph G. Suppose you want to check G for the existence of cycles. In an undirected graph, if the node under consideration has a black or grey neighbor, it indicates a cycle (and the DFS does not visit it as you mention). However, in case of a directed graph, a black neighbor does not mean a cycle. For example, consider a graph with 3 vertices - A,B, and C, with directed edges as A→B, B→C, A→C. Suppose the DFS starts at A, then visits B, then C. When it has returned to A, it then checks that C has already been visited and is black. But there is no cycle in the graph.

In a directed graph, a cycle is present if and only if a node is seen again before all its descendants have been visited. In other words, if a node has a neighbor which is grey, then there is a cycle (and not when the neighbor is black). A grey node means we are currently exploring its descendants - and if one such descendant has an edge to this grey node, then there is a cycle. So, for cycle detection in directed graphs, you need to have 3 colors. There could be other examples too, but you should get the idea.

Wednesday, October 2, 2013

c/c++: the good old returning local variable

 

http://stackoverflow.com/questions/423186/since-i-cant-return-a-local-variable-whats-the-best-way-to-return-a-string-fr

From what I've seen, this should work as expected:

void greet(){
char c[] = "Hello";
greetWith(c);
return;
}

but this will cause undefined behavior:

char *greet(){ 
char c[] = "Hello";
return c;
}
 
 

You're absolutely right. Your c array in the second example is being allocated on the stack, and thus the memory will get reused immediately following. In particular, if you had code like

 printf("%s\n",greet());

you'd get weird results, because the call to printf would have reused some of the space of your array.

The solution is to allocate the memory somewhere else. For expample:

char c[] = "Hello";

char * greet() {
return c;
}

Would work. Another choice would be to allocate it statically in scope:

char * greet() {
static char c[] = "Hello";
return c;
}

because static memory is allocated separately from the stack in data space.

Your third choice is to allocate it on the heap via malloc:

char * greet() {
char * c = (char *) malloc(strlen("Hello")+1); /* +1 for the null */
strcpy(c, "Hello");
return c;
}

but now you have to make sure that memory is freed somehow, or else you have a memory leak.

Tuesday, October 1, 2013

Java: Can’t create generic array of type T

 

http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java
 
private T[] elements = new T[initialCapacity];

 


 


It's because Java's arrays (unlike generics) contain, at runtime, information about its component type. So you must know the component type when you create the array. Since you don't know what T is at runtime, you can't create the array.

Java:The meaning of T extends Comparable<T>

 

Review this:

http://stackoverflow.com/questions/8537500/java-the-meaning-of-t-extends-comparablet

 

This means that the type parameter must support comparison with other instances of its own type, via the Comparable interface.

An example of such a class is provided in the Oracle tutorial Object Ordering.