r/dailyprogrammer • u/nint22 1 2 • May 15 '13
[05/08/13] Challenge #124 [Intermediate] Circular Graphs
(Intermediate): Circular Graphs
A classic problem in computer science & graph-theory is to detect if there are any circular paths. Your goal is to write a program that takes in a series of edges, which defines a graph, and then print all sets of cycles onto a console or text file.
For the sake of clarity, we define a cycle as a set of vertices that have at least one incoming edge and one outgoing edge, where each node is only directly connected to at most two other nodes within the list.
Author: nint22
Formal Inputs & Outputs
Input Description
You will first be given an integer N, which represents the number of edges that will be given on each following new-line. Edges are defined as two integer numbers, where the direction of the edge always goes from the left vertex to the right vertex.
Output Description
Simply print all vertices in a directed cycle; make sure that the cycle is closed (see sample output).
Sample Inputs & Outputs
Sample Input
4
1 2
2 3
3 1
3 4Sample Output
1 2 3 1Note
As usual with these kind of problems, the challenge isn't in writing a solution, but writing a fast-solution. If you post a solution, please discuss the big-O notation of your search function. Good luck, and have fun programming!
2
u/5hassay May 15 '13
might want to add that ) into the link for circular paths, :)
2
u/5hassay May 15 '13
I am unfamiliar with graph theory, so if anyone could help me understand this, that'd be really cool. This is my understanding... The graph might look something like this. Also, 1 2 3 1 is a circular path because I can start at vertex 1 and make my way to it again by going through unique vertices, specifically doing (1)->(2)->(3)->(1).
2
u/eruonna May 15 '13
This is both horribly unclear and probably incorrect.
4
u/skeeto -9 8 May 15 '13
JavaScript. Some data structures: Vertex and Graph prototypes.
Here are the cycle-detection methods. It's just a depth-first search. When it runs into a vertex that's being visited previously on the stack, it records a cycle.
Usage,
3
u/[deleted] May 17 '13 edited May 19 '13
Here is my haskell solution using STRefs
using the sample input i get
using NUNTIUMNECAVI's pastebin file i get
edit: above code doesnt work! (second result from NUNTIUMNECAVI is wrong) :(
edit: correction!.. it works I expectet wrong result because of faulty understanding of tarjans algorithm
2
u/NUNTIUMNECAVI May 15 '13 edited May 15 '13
Python, using Tarjan pretty much directly off Wikipedia:
Sample run on sample input:
Edit: Generated a larger example. Pastebin. Output:
2
u/Coder_d00d 1 3 May 16 '13
Objective-C solution using Apple's foundation framework.
I am using Tarjan's algorithm to find the strongly connected components. It works in O(V+E) where V=vertices and E= Edges.
A Vertex and Graph Class builds up the Adjacency list and has built in data for use in computing the cycles.
I designed the Graph class to be immutable. It is created by adding edges. If any vertex in the edge being added does not exist it adds them and updates adjacency lists as needed. At the end of each Tarjan method run it resets all index data so Tarjan can be run over and over if need be. Edges can also be removed from the Graph if need be.
Output:
2
u/knightry May 16 '13 edited May 16 '13
I think you have to be a little careful here with what you're expecting in the output. Imagine the following graph, presented in ASCII format:
Tarjan's SCC algorithm would find ABCDE to be a SCC, because for each (i,j) in {A,B,C,D,E}, there exists a path i->j && j->i. However, there exist 2 distinct cycles within this set, namely, ABCDA and CDEC.
1
u/SnowdensSecret Jul 02 '13
I know that this is a month late, but I just wrote this solution in Java. I get the expected output for the sample input, but NUNTIUMNECAVI's test produces output radically different from what others are getting. Is there something wrong with my code or did I misunderstand the problem?
Output from the sample input:
Here's where it gets a bit crazy. Output from NUNTIUMNECAVI's file:
That's only part of the output. It found a total of 413 different paths. Obviously, there's something very wrong with my solution. However, I'm not entirely sure what it is.