How to deal with parallel edges between two vertices in cycle detection using BFS in an undirected graph?

I am new to Programming and learning Algorithms and was studying BFS when I read that BFS could be used for cycle detection. I tried to implement the same on an undirected graph G with Adjacency List Representation. What I did is as follows:

• Do a simple BFS Traversal using a Queue while maintaining the parent node of nodes enqueued in the queue.

• If I come across a node u that has a neighbor v such that v is already visited but v is not the parent of u then that means there is cycle in the graph.

Pseudocode:

#adjList is the adjacency list given as a dictionary #myQueue is a double-sided queue containing node and its parent node ([Node, parNode]) #visited is a set containing visited nodes  while(myQueue):     currNode, parNode = myQueue.pop() #dequeue operation     visited.add(currNode) #Marking currNode as visited     for childNode in adjList[currNode]: #Traversing through all children of currNode         if currNode not in visited:             myQueue.appendleft([childNode, currNode]) #Enqueue operation         else:             if childNode!=parNode: #Main logic for cycle detection                 print('CYCLE DETECTED')                 break 

The above approach is working except in cases when I have more than 1 edge between 2 vertices for e.g. in following case we have 2 edges between vertices 0 and 1:

Graph containing cycle

Adjacency list of above graph is: adjList = {0:[1, 2], 1:[0], 2:[0]}. Here we can clearly see that the graph contains a cycle but above algorithm is not able to detect the same because when BFS will reach vertex 1, vertex 0 is already visited but vertex 0 is also the parent of vertex 1 so this cycle will go undetected.

My question is how I can modify above algorithm to detect such cases?

Edit: I tried the same logic on directed graphs also, and I am facing similar problem i.e. case when I have a directed edge from vertex 0 to vertex 1 and another directed edge from vertex 1 to vertex 0

Determine the number of self-crossings for an irregular orientation of an n-pointed star, whose vertices lie on the boundary of a circle

I need to create an algorithm that will output the number of self-crossings for an irregular orientation of an n-pointed star, whose vertices lie on the boundary of a circle. The sample input is as follows:

5 24.0 168.0 312.0 96.0 240.0 

Where the first integer is n (the number of vertices), proceeded with n lines, each describing the positions of the vertices, taken in order, which form the star. Each vertex is in a unique position on the boundary of the unit circle, specified in degrees from the normal axis. All degree measures will be in the range [0,360).

The sample output for the sample input would be:

5 crossings 

To tackle this problem, I was thinking of maybe separating the unit circle into quadrants and determine whether there are crossings depending on the positions of the vertices in their respective quadrants. However, I haven’t been able to think of a way to implement this.

I was wondering if anyone could provide perhaps a high-level idea or a push in the right direction which could help me think of an algorithm.

Thank you.

Like transitive reduction, but removing vertices rather than edges?

Suppose I have a directed graph $ G = (V, E)$ (or, which is the same, a relation on the set $ V$ as defined by the adjacency matrix) that may contain three vertices $ x, y, z$ such that $ xy, xz, yz \in E$ — that is to say, the relation restricted to $ x, y, z$ is transitive, there is a triangle. Let us call this situation a “local transitivity”. My goal is to obtain all the subgraphs of $ G$ induced by cutting middle vertices from local transitivities until none remain, which I dub “resolution”.

There may be multiple solutions. For instance, consider a graph given by this relation:

  a b c d   a □ ■ ■ □   b □ □ ■ ■   c □ □ □ ■   d □ □ □ □   

(It looks like a square with one diagonal.)

There are two ways it can be resolved:

  a b d        a c d   a □ ■ □      a □ ■ □   b □ □ ■      c □ □ ■   d □ □ □      d □ □ □   

One way I can compute the resolutions of a graph is by giving a “non-deterministic” function $ f :G \rightarrow \{G\}$ that removes any single local transitivity. Repeatedly applying $ f$ to any graph, I will eventually converge to a set of induced subgraphs completely devoid of local transitivities.

One way to define $ f$ is by considering all the triples of distinct vertices and checking them for local transitivity. From each matching triple, the middle vertex is extracted, and any of these vertices is cut. But there is about $ |V|^3$ such triples.

Is there a better way? Is there prior art that I may study?

How to draw a graph whose vertices are elements of permuation group

How to write the following program in C++:

Consider the Permutation Group $ S_3$ .

The elements of $ S_3$ are $ e,(12),(13),(23),(123),(132)$ .

I want to draw a graph $ G$ whose members are the elements of $ S_3$ and two vertices $ x,y$ are adjacent if and only if $ xy\neq yx$ .

I am stuck in doing the following things:

  1. How to call the elements of $ S_3$ through a loop?
  2. How to draw the graph $ G$ ?
  3. How to check whether they commute or not

    I am stuck in the three things. Is there any way to write the code in C++?

As an example if I input $ S_3$ I want to get the following graph

$ G=Graph({1:[2,3,4,5],2:[1,3,4,5],3:[1,2,4,5],4:[1,2,3],5:[1,2,3]})

Any help will be highly appreciated.

Directed Trees: Finding all the edges and vertices in a specific direction

I am an electrical engineer without experience in graph theory. However, I have a problem which I believe can be solved by graph theory. We have a directed tree, such as the one below. We want to find all the vertices and edges starting from a vertex in a given direction. For instance, in the figure below, we want to identify the vertices and edges downward from vertex 1 (the red lines). Is there an algorithm to do this (preferably in MATLAB)? The edges have equal weight.

enter image description here

Ordering vertices of graph based on specific vertex-transitivity

Given any complete directed weighted graph $ G$ with $ n$ vertices $ V$ and matrix of non-unique weights $ W$ , find a weak ordering $ T$ on the vertices of the graph satisfying the following conditions:

  • $ T$ is invariant under permuting the vertices

  • For any two vertices $ u$ and $ v$ , $ u = v$ in $ T$ if and only if there is some automorphism $ f:V(G) \rightarrow V(G)\ $ such that $ f(u) = v$ and $ f(v) = u$

The reason I want such a thing is to have some consistent but arbitrary way to order the vertices of a graph that is invariant under permutation, as a tie-breaker for more meaningful orderings, when the vertices are actually distinct, which will usually be the case. If two vertices cannot be distinguished, picking whichever one happens to be first in my representation is fine.

I expect this problem is in $ NP$ , as it seems like there’s some way to compute graph isomorphisms with it, but my graphs are small and I can precompute the ordering once, so it would still be useful to figure out how to do it.

Generating all connected graphs from a set of n-valent vertices?

I would like to have a function generateConnected[list_] that, given a set of vertices with pre-defined valence (number of outgoing edges) generates all possible connected diagrams.

For example, let us choose the following names for vertices of valence 1 through 6:

vertexNames = { x , u , y , z , q , w }; 

which means, a vertex of label x can have only one edge attached to it, u can have only 2 edges, y can have only three edges etc.

Then a set of vertices might be chosen e.g. as follows, so that the output is

set = Flatten[{Array[x, 5], y , z }] generateConnected[set,vertexNames] 

{ x[1] , x[2] , x[3] , x[4] , x[5] , y , z }

enter image description here

Is there an efficient way to do this in Mathematica?

Intersection of sphere with triangle containing moving vertices

First off, apologies if I cannot properly articulate my question in the most formal way. However, I believe my question should be simple enough to grasp anyhow.

In $ \mathbb{R}^3$ , I would like to determine the time of contact, if any, between:

  • An unmoving unit sphere, whose center is at the origin
  • A triangle, each of whose vertices follow independent, linear motion from $ t = 0$ to $ t = 1$ . In other words, each triangle vertex has a start and end position, which are linearly interpolated by $ t$ .

The sphere may touch the triangle on a vertex, an edge, or on the triangle’s surface. Vertex testing is simple enough as it’s analogous to static line segment-sphere intersection.

For edge testing, parameterizing a line-sphere intersection test by $ t$ appaers to lead to solving a degree 4-polynomial, which isn’t ideal. I believe that doing the same for triangle surface testing (parameterizing a sphere-plane intersection with $ t$ ) would lead to solving a 6-degree polynomial.

Would there be any applicable non-analytical methods to approximate the intersection (other than directly approximating the polynomial roots)? Or maybe there is an analytical method that I’m not thinking of. In addition, would further constraining the motion of the triangle potentially simplify a solution?

A vertex transitive graph has a near perfect/ matching missing an independent set of vertices

Consider a power of cycle graph $ C_n^k$ , represented as a Cayley graph with generating set $ \{1,2,\ldots, k,n-k,\ldots,n-1\}$ on the Group $ \mathbb{Z}_n$ . Supposing I remove an independent set of vertices of the form $ \{i,i+k+1,\ldots,\lfloor\frac{n}{k+1}\rfloor+i\}$ or a single vertex. Then, is it possible to obtain a perfect/ near perfect matching when I remove the independent set of vertices always? If not, then is it possible in case the graph is an even power of cycle?

I hope yes, as we can pair the vertices between any two independent sets of the above form or between the indpendent set and the single vertex to get a maximal matching which is near perfect(in case the order of induced subgraph is odd) or perfect(in case the order of the induced subgraph is even). Any counterexamples? Also, can we generalize this, if true, to any vertex transitive graph, that is, does there exist an indpendent set(non-singleton) of vertices, such that removing that set induces a perfect/near-perfect matching? Thanks beforehand.

Tooltip pointing for 3D polyhedron vertices

How is possible to use Tooltip to point out vertices of a polyhedron? Would like coordinate display while mousing over the vertex. Thanks for the help.

Rohn = PolyhedronData["Icosidodecahedron", "VertexCoordinates"]; DelaunayMesh[Rohn] Show[ListPlot3D[Rohn, AxesStyle -> Thickness[0.005],    AxesLabel -> {"X", "Y", "Corr(X,Y)"},    AxesEdge -> {{-1, -1}, {-1, -1}, {-1, -1}}],   ListPointPlot3D@   Tooltip@Flatten[MapIndexed[Flatten@{#2, #1} &, Rohn, {2}], 1]]