cyclic-graph

How to handle a path in Prolog graph traversal

▼魔方 西西 提交于 2020-01-05 17:58:55
问题 I have written in Prolog: edge(x, y). edge(y, t). edge(t, z). edge(y, z). edge(x, z). edge(z, x). path(Start, End, Path) :- path3(Start, End, [Start], Path). path3(End, End, RPath, Path) :- reverse(RPath, Path). path3(A,B,Path,[B|Path]) :- edge(A,B), !. path3(A, B, Done, Path) :- edge(A, Next), \+ memberchk(Next, Done), path3(Next, B, [Next|Done], Path). Its taking care of cyclic graphs as well, I am getting an irregular output when I try to traverse same node from same node. eg: path(x,x,P).

Is there a way to build a structure with cyclic links without runtime overhead?

前提是你 提交于 2019-12-24 08:48:04
问题 I am trying to implement a cyclic linked data structure in Rust. My Node s are defined as: #[derive(Debug)] enum Node<'a> { Link(&'a Node<'a>), Leaf, } I am trying to build a minimal structure like this (extra brackets for better lifetime visibility): fn main() { let placeholder = Node::Leaf; { let link1 = Node::Link(&placeholder); { let link2 = Node::Link(&link1); println!("{:?}", link2); } // link2 gets dropped } // link1 gets dropped } This works, but now I don't know how to replace the

couting back edges to get the number of cylces in a directed graph

断了今生、忘了曾经 提交于 2019-12-23 18:13:46
问题 I have been writing code to get all the possible cycles in a directed graph. Here is an implementation that keeps track of back edges and whenever one back edge is found, it return true that one cycle is detected. I extended this to the following: calculate all the possible back edges in a tree, the number of back edges should give the number of cycles. Not sure if this correct. using this, I implemented the following: The count variable below is not useful. Initially I had it to give the

How to initialize and “modify” a cyclic persistent data structure in Scala?

会有一股神秘感。 提交于 2019-12-17 23:46:35
问题 I have searched and found some info on this topic but the answers are either confusing or not applicable. I have something like this: class Thing (val name:String, val refs:IndexedSeq[Ref]) class Ref (val name:String, val thing:Thing) Now, I want to say, load in a file, parse it and populate this data structure from it. It being immutable and cyclic, how might one do so? Also, let's say I do get this data structure populated, now I want to modify it, like change rootThing.refs(3).name, how

code for cycle detection is not finding returning the right number of cycles in directed graph in Java?

不羁的心 提交于 2019-12-13 21:24:23
问题 I have code written for calculating the no of cycles in a directed graph using DFS . The method to check if a cycle exists or not works fine. I now iterate through all the vertices (which I have in a HashMap) and check if a vertex is unvisited, then check if a cycle exists, if so increment the counter by 1. Now the code breaks, it does not give the right number of cycles eg: for the graph with following edges: (A B),(B C),(C E),(E A),(B E) Here is my code; public int getTotalCyclesinDir(){

Prolog graph path search with cyclic path

血红的双手。 提交于 2019-12-01 08:46:40
I am a complete newbie in Prolog. I am trying to figure out a problem where I need to check if path is present between edges. I am done with acyclic graph code for cyclic my code is going to infinite loop. path(Start, End) :- edge(Start, End). path(Start, End) :- edge(Start, Z), path(Z, End). I need to handle this case by defining a new predicate: new_path(Start,End,path) which should eliminate infinite loop. Please let me know how to proceed with it. false Try path(Start, End) :- closure(edge, Start, End). using this definition for closure/3 Nicholas Carey You need to keep track of what nodes

Prolog graph path search with cyclic path

帅比萌擦擦* 提交于 2019-12-01 07:04:36
问题 I am a complete newbie in Prolog. I am trying to figure out a problem where I need to check if path is present between edges. I am done with acyclic graph code for cyclic my code is going to infinite loop. path(Start, End) :- edge(Start, End). path(Start, End) :- edge(Start, Z), path(Z, End). I need to handle this case by defining a new predicate: new_path(Start,End,path) which should eliminate infinite loop. Please let me know how to proceed with it. 回答1: Try path(Start, End) :- closure(edge

How to represent directed cyclic graph in Prolog with direct access to neighbour verticies

為{幸葍}努か 提交于 2019-11-29 11:24:06
I need to construct directed graph (at runtime) with cycles in Prolog and I am not sure know how to represent it. My requirement is that I need to get from one vertex to his neigbour in a constant time. Is it possible to represent it as a tree, e.g.: t(left_son,V,right_son) but how to solve the cycles? I can make a list of edges: graph([a,b,c,d],[e(a,b),e(b,c),e(c,a),e(c,d)]) OR just [a->[b],b->[c],c->[a,d],d->[]] but how to avoid calling function 'member' on list while searching for neighbours, which cost linear time? Thanks for your help If your graph has less vertices than max arity allowed

How to detect if adding an edge to a directed graph results in a cycle?

旧街凉风 提交于 2019-11-28 05:47:01
I came upon wait-for graphs and I wonder, are there any efficient algorithms for detecting if adding an edge to a directed graph results in a cycle? The graphs in question are mutable (they can have nodes and edges added or removed). And we're not interested in actually knowing an offending cycle, just knowing there is one is enough (to prevent adding an offending edge). Of course it'd be possible to use an algorithm for computing strongly connected components (such as Tarjan's) to check if the new graph is acyclic or not, but running it again every time an edge is added seems quite

How to represent directed cyclic graph in Prolog with direct access to neighbour verticies

二次信任 提交于 2019-11-28 04:49:21
问题 I need to construct directed graph (at runtime) with cycles in Prolog and I am not sure know how to represent it. My requirement is that I need to get from one vertex to his neigbour in a constant time. Is it possible to represent it as a tree, e.g.: t(left_son,V,right_son) but how to solve the cycles? I can make a list of edges: graph([a,b,c,d],[e(a,b),e(b,c),e(c,a),e(c,d)]) OR just [a->[b],b->[c],c->[a,d],d->[]] but how to avoid calling function 'member' on list while searching for