DFS on a graph using a python generator

2024/11/18 15:39:29

I am using a generator to do a full search on a graph, the real data set is fairly large, here is a portion of the code i wrote on a small data set:

class dfs:def __init__(self):self.start_nodes = [1,2]  # not used, see below explanationself.end_nodes = [5,7] # not used, see below explanation_graph={1 : [4,5,6,2],2 : [1,3,5],4 : [1,5],3 : [5,2],5 : [3,1,4,6,2,7],6 : [1,5],7 : [5],}def __iter__(self):return self.find_path(self._graph, 2, 7)def find_path(self, graph, start, end, path=[]):path = path + [start]if start == end:yield pathif not graph.has_key(start):returnfor node in graph[start]:if node not in path:for new_path in self.find_path(graph, node, end, path):if new_path:yield new_pathd = dfs()
print list(d)

when run this outputs all the paths from '2' to '7' as expected:

[[2, 1, 4, 5, 7], [2, 1, 5, 7], [2, 1, 6, 5, 7], [2, 3, 5, 7], [2, 5, 7]]

What I would like to do is modify this generator so that it does the same thing except i get the paths back for a set number of start and end points, ie self.start_nodes and self.end_nodes.

Since my generator is a recursive function it makes it difficult to loop on the different start and end points, been scratching my head over this any ideas?

Answer

Perhaps I'm misunderstanding your question, but it seems to me that you want to replace your __iter__ function with something like this:

def __iter__(self):for start in self.start_nodes:for end in self.end_nodes:for path in self.find_path(self._graph, start, end):yield path

You can find more information about generators in this question.

https://en.xdnf.cn/q/118657.html

Related Q&A

Importing test libraries failed. No module named a

I have a folder /example which contains /Libs which further contains different folders /a, /b each containing python libraries. I am trying to run a robot framework code from /example.The error it show…

Sorting characters by count using PHP or Python

I have a string of charactersabcdefghijklmnopqrstuvwxyz_ I want to take this string of characters and sort them by the number of times they appear in a large block of characters. For example: cwrxwzb…

Save user first_name as default value for model django

I have an article model with author variable which I want to save as the users first and last name. I use custom user model called Account. author = models.CharField(author,max_length=50 default=User.f…

How to compare two imagefile from two different files in python

I would like to create a program that compares two images. I need to take images from two different folders and compare that images if they are same or not. Then I want to print out as same or differen…

Cant import from module despite presence of __init__.py

I have the following folder structureproject_folder/pyutils/__init__.pyscript1.pyscript2.py lambdas/__init__.pylambda_script1.pylambda_script2.pylambda_tests/__init__.pylambda_test1.pyWithin lambda_tes…

Multiple strings in one variable

Say, for example, I have some code that goes like this: sentence = input("Enter input: ") target_letter = "a" or "b" or "c" print(sentence.index(target_letter))M…

How to get the area of shape filled using turtle

I graphed a fractal shape in Python using turtle, and am trying to get the area of this fractal after a sufficiently high iteration. This fractal is related to the Koch snowflake, for those interested.…

What distinguishes a command from needing () vs not?

I recently spent way too long debugging a piece of code, only to realize that the issue was I did not include a () after a command. What is the logic behind which commands require a () and which do not…

python iterate yaml and filter result

I have this yaml file data:- name: acme_aws1source: awspath: acme/acme_aws1.zip- name: acme_gke1source: gkepath: acme/acme_gke1.zip- name: acme_ocisource: ocipath: acme/acme_oci1.zip- name: acme_aws2so…

Faster way to looping pixel by pixel to calculate entropy in an image

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size. Here is my func…