question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in NaiveLcaFinder

See original GitHub issue

Hello all, while trying to parse a dot file I used a graph whose all LCAs should be Robert and Cersei but instead, received nothing. Please check if it really is a bug. This bug happens to be in function findLcas of NaiveLcaFinder.

@Test
    public void testPseudoLcas(){
     public final String NL = "\n";
    	String input = "digraph GOT {"+NL+
			"graph [ bgcolor = whitesmoke ]"+NL+
			"subgraph cluster_stark {"+NL+
				"style = filled ;"+NL+
				"color = lightblue ;"+NL+
				"label = \" House Stark \" ;"+NL+
				"node [ style = filled , color = white ];"+NL+
				"Rickard ;"+NL+
				"Brandon ; Eddard ; Benjen ; Lyanna ;"+NL+
				"Robb ; Sansa ; Arya ; Brandon ; Rickon ;"+NL+
				"node [ shape = doublecircle , style = filled , color = white ];"+NL+
				"Jon ;"+NL+
				"Rickard -> Brandon ;"+NL+
				"Rickard -> Eddard ;"+NL+
				"Rickard -> Benjen ;"+NL+
				"Rickard -> Lyanna ;"+NL+
				"Eddard -> Robb ;"+NL+
				"Eddard -> Sansa ;"+NL+
				"Eddard -> Arya ;"+NL+
				"Eddard -> Brandon ;"+NL+
				"Eddard -> Rickon ;"+NL+
				"Eddard -> Jon [ label = \" bastard \" , color = azure4 ];"+NL+
			"}"+NL+
			"subgraph cluster_baratheon {"+NL+
				"style = filled ;" +NL+
				"color = chocolate3 ;" +NL+
				"label = \" House Baratheon \" ;" +NL+
				"node [ style = filled , color = white ];" +NL+
				"Ormund ; Steffon ; Robert ; Stannis ; Renly ; Shireen ; Joffrey ; Myrcellar ; Tommen ;" +NL+
				"Ormund -> Steffon ;" +NL+
				"Rhaelle -> Steffon ;" +NL+
				"Ormund -> Rhaelle ;" +NL+
				"Rhaelle -> Ormund ;" +NL+
				"Steffon -> Robert ;" +NL+
				"Steffon -> Stannis ;" +NL+
				"Steffon -> Renly ;" +NL+
				"Stannis -> Shireen ;" +NL+
				"Robert -> Joffrey ;" +NL+
				"Robert -> Myrcellar ;" +NL+
				"Robert -> Tommen ;" +NL+
			"}" +NL+
			"subgraph cluster_lannister {"+NL+
				"style = filled ;"+NL+
				"color = cornsilk3 ;"+NL+
				"label = \" House Lannister \" ;"+NL+
				"node [ style = filled , color = white ];"+NL+
				"Tywin ; Joanna ; Jaime ; Cersei ; Tyrion ;"+NL+
				"Tywin -> Joanna ;"+NL+
				"Joanna -> Tywin ;"+NL+
				"Joanna -> Jaime ;"+NL+
				"Joanna -> Cersei ;"+NL+
				"Joanna -> Tyrion ;"+NL+
				"Tywin -> Jaime ;"+NL+
				"Tywin -> Cersei ;"+NL+
				"Tywin -> Tyrion ;"+NL+
				"Jaime -> Cersei ;"+NL+
				"Cersei -> Jaime ;"+NL+
				"Robert -> Cersei ;"+NL+
				"Cersei -> Robert ;"+NL+
				"Cersei -> Joffrey ;"+NL+
				"Cersei -> Myrcellar ;"+NL+
				"Cersei -> Tommen ;"+NL+
				"Jaime -> Joffrey [ style = dashed ];"+NL+
				"Jaime -> Myrcellar [ style = dashed ];"+NL+
				"Jaime -> Tommen [ style = dashed ];"+NL+
			"}"+NL+
			"Lyanna -> Rhaegar [ style = dashed , label = \" ? \" ];"+NL+
			"Rhaegar -> Lyanna [ style = dashed , label = \" ? \" ];"+NL+
			"Lyanna -> Jon [ style = dashed , label = \" ? \" ];"+NL+
			"Rhaegar -> Jon [ style = dashed , label = \" ? \" ];"+NL+
			"labelloc = \" t \" ;"+NL+
			"fontsize =50;"+NL+
			"fontcolor = lightslategrey ;"+NL+
			"fontname = \" Bookman Old Style Bold Italic \" ;"+NL+
			"label = \" Game of Thrones Family Tree \""+NL+
			"}" ; 
    	
            VertexProvider<String> vp = (a, b) -> a;
	    EdgeProvider<String, DefaultEdge> ep = (f, t, l, a) -> new DefaultEdge();
	    GraphImporter<String, DefaultEdge> importer = new DOTImporter<String, DefaultEdge>(vp, ep);
	    DirectedPseudograph<String, DefaultEdge> graph = new DirectedPseudograph<String, DefaultEdge>(DefaultEdge.class);
	    try {
			importer.importGraph(graph, new StringReader(input));
		} catch (ImportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	NaiveLcaFinder<String, DefaultEdge> graphFinder = new NaiveLcaFinder<>(graph);
        checkLcas(graphFinder, "Joffrey", "Tommen", Arrays.asList("Robert"));
    }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
AlexandruValeanucommented, Feb 20, 2018

@tibrewalpratik17 The graph in that pdf is not a DAG so you don’t have to assume anything: it is not a valid input to NaiveLcaFinder.

For the warmup challenge, it’s better to simply modify the graph so that it no longer contains any cycles. You can use CycleDetector to check whether there are any cycles in your version of the graph.

My implementation of findLcas doesn’t try to detect cycles or do anything smart to produce a sensible answer when given a cyclic graph (i.e. you get undefined behaviour). Therefore there is no point in analysing the output if the precondition was broken.

1reaction
AlexandruValeanucommented, Feb 18, 2018

@hulk-baba You are right. It doesn’t work in that case.

A set of common ancestors would be [Robert, Cersei, Jaime]. But then this happens. That piece of code removes all nodes that have a child in the set of ancestors. Since there are edges from Cersei to Robert and Jaime and from Robert to Cersei all three of them can (and will) be eliminated from the set of common ancestors (Cersei is eliminated because of Robert, Jaime because of Cersei and Robert because of Cersei).

This is not a problem with the actual implementation as the graph is not a DAG (the cycle between Robert and Cersei creates problems in the Lannister/Baratheon subgraph).

Possible fixes:

  • Remove the edge from Robert to Cersei; I get “Robert” after I’ve removed the edge
  • In this case, findLca works. It might not work on other cycles but it seems to work on this one
  • There is a small fix I could make so that it performs betters on graphs with cycles but this is not the purpose of this implementation
Read more comments on GitHub >

github_iconTop Results From Across the Web

Look Up! The Billion-Bug Highway You Can't See - NPR
For a variety of reasons bugs disperse. You can see them launching themselves, says entomologist Matt Greenstone:
Read more >
350 questions with answers in JAVA | Science topic
I am interested in calculating the lowest common ancestors of several nodes in a directed graph. I tested the findlcas method proposed by...
Read more >
DefaultListenableGraph.java example - Javatips.net
This class describes the usage of DefaultListenableGraph.java.
Read more >
Patch-Datei herunterladen
jgrapht/alg/class-use/NaiveLcaFinder.html | 117 - . ... uwp7R4gv5idFxqKP3-buG#&f-`44izOn|@)7K&bq8!QS-@ePYcOe+5aQC7 ...
Read more >
jgrapht - NaiveLcaFinderのバグ | bleepcoder.jp
みなさん、こんにちは。ドットファイルを解析しようとしているときに、すべてのLCAが Robert と Cersei であるはずのグラフを使用しましたが、代わりに何も受け取り ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found