Loop detection in connected graph - loops

Most example I found only treat single linked lists. I need a solution for a multiple linked list.
Image is easier (valid):
Invalid:
Which algorithm would be able to return the begining of the loop (B) and not collide with E? A good starting point would be also to know if there is a loop at all.
Stuff like this or edge counting doesn't work (because not single linked...).
Thanks.

Just check if a route from 'end of connection node (B)' to 'start of connection node (C)' exists, if yes, a new loop would be created. Doesn't quite answer it, but good enough...

Related

Solve maze using queue data structure?

I'm taking a class in data structures and was given the assignment to find the shortest path through a maze using C and implementing the queue data structure. However, I can't really wrap my head around how to use a queue here.
I know the idea is to count every possible move from the start position, and when you hit the target, you're supposed to trace back to the initial position. This is what I don't understand. Because if I use a queue and delete all the moves that leads up to the target, I have no data to use to do the trace back, and if I don't delete the moves that lead to the target (i.e. saving all the possible moves and deleting them when I actually do the trace back), I might as well be using a stack.
I know there's something I don't quite get, but I can't figure out what it is. How would I utilize the queue data structure in this case?
What your professor is trying to get you to use is called "breadth-first search". The queue comes in for deciding which spaces to explore next. When you are looking at the possible paths to take, you enqueue all the paths you have yet to explore. Instead of continuing down the path you're on (which would be "depth-first search"), you dequeue the next spot you need to check, which will take you back to one of the positions you were considering earlier.
The actual implementation is up to you, I'd recommend looking for examples of breadth-first search online.

Beginners Example: Cypher not returning a node name for one node, but is for all the others

I'm trying to learn about Cypher using the really basic, beginners example at http://www.neo4j.org/console.
The first query, I attempted is:
MATCH (n)
RETURN n
which essentially says:
"Match all nodes and return them"
The result I get is this:
{name:"Neo"}
(1:Crew {name:"Morpheus"})
(2:Crew {name:"Trinity"})
(3:Crew:Matrix {name:"Cypher"})
(4:Matrix {name:"Agent Smith"})
(5:Matrix {name:"The Architect"})
This answer seems correct, as these are indeed all the nodes in the database. However, why does the result for "Neo" not include a number and the type "Crew" like all the others?
I would expect it to say (0:Crew {name:"Neo"})
In fact it should ...
I have checked ajax call, and I have found the label for this node, but neo4j console doesn't print it. I don't know why for now.
I think this is a bug of the application.
Can you report it here https://github.com/neo4j-contrib/rabbithole ?
Cheers.

Linked List insert in C

I have recently started working with linked lists. To push an element into linked list in the insert(...) function, I saw we always check if(head == NULL) but it occurs only once.
I want to know if there is any way so that we can avoid the unnecessary check always. Please suggest something that would be relevant to most of the linked list operations. One solution I figured out is that writing a new function "add_first_element(....)" so that explicitly we add the first element and then other elements are added in a generic way.
I am looking for a better solution.
A common way is to use a sentinel node. That is, a node that contains no useful data, but merely serves as the placeholder for the one before the first node. This way you don't need to check for null.
For double-linked list, you will need two sentinel nodes to avoid null checking.

Graph-Traversal: How do I query for "friends and friends of friends" using Gremlin

In my graph database I have Branches and Leaves. Branches can "contain" Leaves and Branches can "contain" Branches.
How, using Gremlin, can I find all leaves for a given branch, that are directly or indirectly related to it?
I got this to work in Cypher:
START v=node(1) MATCH v-[:contains*1..2]->i RETURN v,i
Where the *1..2 means "friends and friends of friends".
I thought maybe LoopV was the way forward, but I just get an Exception:
Error reading JArray from JsonReader. Current JsonReader item is not an array: String
You can do the following in Gremlin 1.4+.
g.v(1).out('contains').loop(1){true}{it.out('contains').count() == 0}
This says:
Start at vertex with id 1
Take the outgoing "contains" edges.
Loop over the out('contains') section.
Loop "infinitely" (make sure your tree doesn't have loops in it)
Emit only those vertices touched that don't have more outgoing 'contains'-edges. (i.e. the leaves)
However, looking at what you wanted from Cypher, it looks like you only want 2 steps. Thus, to do that, simply do:
g.v(1).out('contains').loop(1){it.loops < 3}
Perhaps I misunderstood your question --- either way, that should give you enough to play with.

How to randomly access a point in a CvSeq?

Can we randomly access a point in a CvSeq object? We can traverse it, so I imagine it's possible in a simple manner. How is this accomplished?
I have found it. There is a method called cvGetSeqElem, which takes in the sequence and the index. Thanks for the help though. This might just follow the linked list linearly, but it's simpler than manually coding the search.
Looking at the OpenCV API (http://opencv.willowgarage.com/documentation/dynamic_structures.html) it doesn't sound possible. Looks to be some form of linked list implementation, which means that the only way to access an element part way though is to follow the links.
cvSeq is a linked list - you have to follow the chain of links, you have no idea where the next entry is stored in memory.

Resources