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

  • Home
  • SEARCH
  • 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 8987631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:50:38+00:00 2026-06-15T21:50:38+00:00

I have the following Python code, and it works perfect with the directed graph.

  • 0

I have the following Python code, and it works perfect with the directed graph.
My question is how to modify the code in such way to find all the paths with ignoring the directions of the edges.

For example, if we have the following connections:

1->2

3->2

My code will not return a path between 1 and 3, which is expected as it is directed. But with ignoring the directions of edges the code should find a path from 1 to 3.

I want the code to ignore the direction and finds all the paths between the two given nodes.

I tried the proposed solution and it works really nice, the solution is: “The simplest solution might be to pre-process your graph by adding the arc B->A for every A->B that’s in the graph.“

What I really wanted is to modify the algorithm itself to deal with the graph as is.

Python Code:

# a sample graph
graph = {'A': ['B', 'C','E'],
             'B': ['A','C', 'D'],
             'C': ['D'],
             'D': ['C'],
             'E': ['F','D'],
             'F': ['C']}

class MyQUEUE: # just an implementation of a queue

    def __init__(self):
        self.holder = []

    def enqueue(self,val):
        self.holder.append(val)

    def dequeue(self):
        val = None
        try:
            val = self.holder[0]
            if len(self.holder) == 1:
                self.holder = []
            else:
                self.holder = self.holder[1:]   
        except:
            pass

        return val  

    def IsEmpty(self):
        result = False
        if len(self.holder) == 0:
            result = True
        return result


path_queue = MyQUEUE() # now we make a queue


def BFS(graph,start,end,q):

    temp_path = [start]

    q.enqueue(temp_path)

    while q.IsEmpty() == False:
        tmp_path = q.dequeue()
        last_node = tmp_path[len(tmp_path)-1]
        #print tmp_path
        if last_node == end:
            print "VALID_PATH : ",tmp_path
        for link_node in graph[last_node]:
            if link_node not in tmp_path:
                new_path = []
                new_path = tmp_path + [link_node]
                q.enqueue(new_path)

BFS(graph,"A","D",path_queue)

————-Output of the Code——————-

['A', 'B', 'D'] 
['A', 'C', 'D']
['A', 'E', 'D']
['A', 'B', 'C', 'D']
['A', 'E', 'F', 'C', 'D']

Note: I taged Java, in case someone has a solution to the same problem in Java

  • 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-15T21:50:39+00:00Added an answer on June 15, 2026 at 9:50 pm

    The simplest solution might be to pre-process your graph by adding the arc B->A for every A->B that’s in the graph. Then you should be able to use your algorithm as-is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following python code, it works fine in python but fails with
I have the following code that works in Python 2.7: entry_regex = '(' +
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
So I have the following code GAE code in python and it is a
I'm working in Python. I have the following code: while not is_suffix(pattern[:k], pattern[:q]): k
I'm just learning about python. I'm fairly new. I have the following code that
I have the following python modules. Sorry if the code is ugly. This is
I have the following chunk of python code: import hashlib class User: def _set_password(self,
I have the following python code: try: pr.update() except ConfigurationException as e: returnString=e.line+' '+e.errormsg

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.