Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:14:49+00:00 2026-06-17T12:14:49+00:00

I am having some trouble writing an algorithm that returns all the paths forming

  • 0

I am having some trouble writing an algorithm that returns all the paths forming simple cycles on an undirected graph.

I am considering at first all cycles starting from a vertex A, which would be, for the graph below

A,B,E,G,F
A,B,E,D,F
A,B,C,D,F
A,B,C,D,E,G,F

Additional cycles would be

B,C,D,E
F,D,E,G

but these could be found, for example, by calling the same algorithm again but starting from B and from D, respectively.

The graph is shown below –

enter image description here

My current approach is to build all the possible paths from A by visiting all the neighbors of A, and then the neighbors of the neightbors and so on, while following these rules:

  • each time that more than one neighbor exist, a fork is found and a new path from A is created and explored.

  • if any of the created paths visits the original vertex, that path is a cycle.

  • if any of the created paths visits the same vertex twice (different from A) the path is discarded.

  • continue until all possible paths have been explored.

I am currently having problems trying to avoid the same cycle being found more than once, and I am trying to solve this by looking if the new neighbor is already part of another existing path so that the two paths combined (if independent) build up a cycle.

My question is: Am I following the correct/better/simpler logic to solve this problem.?

I would appreciate your comments

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:14:51+00:00Added an answer on June 17, 2026 at 12:14 pm

    Based on the answer of @eminsenay to other question, I used the elementaryCycles library developed by Frank Meyer, from web_at_normalisiert_dot_de which implements the algorithms of Johnson.

    However, since this library is for directed graphs, I added some routines to:

    • build the adjacency matrix from a JGraphT undirected graph (needed by Meyer’s lib)
    • filter the results to avoid cycles of length 2
    • delete repeated cycles, since Meyer’s lib is for directed graphs, and each undirected cycle is two directed cycles (one on each direction).

    The code is

    package test;
    
    import java.util.*;
    
    import org.jgraph.graph.DefaultEdge;
    import org.jgrapht.UndirectedGraph;
    import org.jgrapht.graph.SimpleGraph;
    
    
    public class GraphHandling<V> {
    
    private UndirectedGraph<V,DefaultEdge>     graph;
    private List<V>                         vertexList;
    private boolean                         adjMatrix[][];
    
    public GraphHandling() {
        this.graph             = new SimpleGraph<V, DefaultEdge>(DefaultEdge.class);
        this.vertexList     = new ArrayList<V>();
    }
    
    public void addVertex(V vertex) {
        this.graph.addVertex(vertex);
        this.vertexList.add(vertex);
    }
    
    public void addEdge(V vertex1, V vertex2) {
        this.graph.addEdge(vertex1, vertex2);
    }
    
    public UndirectedGraph<V, DefaultEdge> getGraph() {
        return graph;
    }
    
    public List<List<V>>     getAllCycles() {
        this.buildAdjancyMatrix();
    
        @SuppressWarnings("unchecked")
        V[] vertexArray                 = (V[]) this.vertexList.toArray();
        ElementaryCyclesSearch     ecs     = new ElementaryCyclesSearch(this.adjMatrix, vertexArray);
    
        @SuppressWarnings("unchecked")
        List<List<V>>             cycles0    = ecs.getElementaryCycles();
    
        // remove cycles of size 2
        Iterator<List<V>>         listIt    = cycles0.iterator();
        while(listIt.hasNext()) {
            List<V> cycle = listIt.next();
    
            if(cycle.size() == 2) {
                listIt.remove();
            }
        }
    
        // remove repeated cycles (two cycles are repeated if they have the same vertex (no matter the order)
        List<List<V>> cycles1             = removeRepeatedLists(cycles0);
    
        for(List<V> cycle : cycles1) {
            System.out.println(cycle);    
        }
    
    
        return cycles1;
    }
    
    private void buildAdjancyMatrix() {
        Set<DefaultEdge>     edges        = this.graph.edgeSet();
        Integer             nVertex     = this.vertexList.size();
        this.adjMatrix                     = new boolean[nVertex][nVertex];
    
        for(DefaultEdge edge : edges) {
            V v1     = this.graph.getEdgeSource(edge);
            V v2     = this.graph.getEdgeTarget(edge);
    
            int     i = this.vertexList.indexOf(v1);
            int     j = this.vertexList.indexOf(v2);
    
            this.adjMatrix[i][j]     = true;
            this.adjMatrix[j][i]     = true;
        }
    }
    /* Here repeated lists are those with the same elements, no matter the order, 
     * and it is assumed that there are no repeated elements on any of the lists*/
    private List<List<V>> removeRepeatedLists(List<List<V>> listOfLists) {
        List<List<V>> inputListOfLists         = new ArrayList<List<V>>(listOfLists);
        List<List<V>> outputListOfLists     = new ArrayList<List<V>>();
    
        while(!inputListOfLists.isEmpty()) {
            // get the first element
            List<V> thisList     = inputListOfLists.get(0);
            // remove it
            inputListOfLists.remove(0);
            outputListOfLists.add(thisList);
            // look for duplicates
            Integer             nEl     = thisList.size();
            Iterator<List<V>>     listIt    = inputListOfLists.iterator();
            while(listIt.hasNext()) {
                List<V>     remainingList     = listIt.next();
    
                if(remainingList.size() == nEl) {
                    if(remainingList.containsAll(thisList)) {
                        listIt.remove();    
                    }
                }
            }
    
        }
    
        return outputListOfLists;
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble with writing a unit test that checks my custom
I'm new to linq and having trouble writing two simple queries. For some reason,
I'm having some trouble writing a correct algorithm for centering a background image. Here's
I am having some trouble writing a regex expression that will strip outer brackets
I'm having some trouble writing a GUI in Java that can interact with Tcl
I'm having some trouble writing a relatively simple predicate in Prolog. This predicate is
I am having some trouble writing a script that will find a string iff
Im having some trouble writing a getstring function, this is what I have so
I am having some trouble with writing classes in Arduino. Here is a class
Having some trouble with what should be a very simple scenario. For example purposes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.