Depth First Search in Python - python

I'm having problems in implementing search algorithms in Python. I need a generic depth first search code which can return the path from the start state to end state.

Start with this essay on implementing Graphs in Python and then understand that DFS may use a stack to pop in and pop out nodes of the graph. Having this idea may help you proceed with implementing DFS in Python. And if you have specific questions, please post here and folks are ready to help.

You can find a detailed implementation explanation at literateprograms.org.
(Actually, it's pretty much the first Google hit, trying that before posting questions on SO might help next time)

Use the adjacency list representation to implement the graph. Code to implement it is as follows. (this code is specific if you use Python to read a text file on graph and implement the adjecency list)
(Python 3.3)
def ajlist(nameofgraph,start,goal):
x=input("enter the name of the file you want to implement adjecency list: ")##file.txt##
text_file=open(x,"r")
nameofgraph={}##use a dictionary##
for line in text_file:
(vertex,val)=line.split()
if vertex not in nameofgraph:
nameofgraph[vertex]=set9[val])
else:
nameofgraph.get[key].add(val)
stack=[(stack,[start])]
while stack:
(vertex,path)=stack.pop()
for next in graph[vertex]-set(path):
if next==goal:
yield (path+[next])
else:
stack.append((next,path+[next]))
When you want to run this use this syntax list(DFS(nameofgraph,start,goal))
Above is the easiest way of doing DFS and find the path in graph. If you have implemented a graph in python then you do not need the (input) function. Then you have to do is take away the implementing adjecency list part and use the real traversal part.

Related

Is is still considered a BFS algorithm if I modify it A little bit?

So I'm trying to create a program that finds the shortest path from nodeA to nodeB, however, I want to block certain nodes so that it would find another path. I'm not really aiming for an optimal code here I'm just trying things out, exploring etc.
Is it still considered a BFS if I modify it a little?
Yes, it still is a BFS, with just a few constraints. The essence of BFS algorithm is the way it explores the graph, you are just exploring a subgraph (through filtering out a bit of it).

matplotlib to visualize linked lists and decisions trees

are there any tools or examples for how to visualize things like linked lists and decisions trees using matplotlib?
I ask because I wrote a linked list type of class (each node can have multiple inputs/outputs, and there's a class variable that stores node names), and want to visualize it. Unfortunately, my computer at work is locked down so much that I can't download other packages, so I've got to use whatever is on hand- which is matplotlib
I've started reading it, and if I do it by hand, I can probably make something that visualizes one-directional linked lists (just give it the root node, and plop down a square with text for each operation). But if there's branching, or multiple inputs into a node things get a bit more complicated- for example is it possible to expand a figure after creating it?
Yes, you can use networkx library and the draw_networkx method. There are plenty of examples on Stack Overflow. Here is one example: https://stackoverflow.com/a/52683100/6361531

About the list of all nodes in graph(python)

I meet this question when I read about the python script of SCI2(Science of Science)software. Maybe what I want to ask is a simple python question.
I will show the SCI2 script first in the picture below. After that,I will write some codes. My question is why the codes in the picture can get list of all nodes by slicing method [:] but I can't.
codes from SCI2 python script Maybe the picture can not be shown, I write the important codes here. (the language is python)
# make a copy of the list of all nodes
nodesbynumworks = g.nodes[:]
What I wirte is:
import networkx as nx
g =nx.Graph()
g.add_node(1,size=11)
g.add_node(2,size=12)
a = g.nodes[:]
And this is my result:
my result
I just want to get the list of all nodes so that I can take some nodes from this list. And maybe I can change some attributes of some nodes.But I can't do that now. What I can think about is the reason that python2 and python3 are different in some place. I know this a a very pale explanation.
Hope someone can help me.
If you want the list of keys, then try:
g.nodes().keys()
If you want the list of values, then try:
g.nodes().values()
For SCI2, it appears to me that they have made a choice about how to represent graphs. For them graph.nodes is a list (or perhaps it's a numpy array or something similar). Thus graph.nodes[:] is a perfectly well-defined command.
You're using networkx for your graphs. For networkx graph.nodes is something different. In earlier versions of networkx, it is a function that returns a list of nodes, so graph.nodes()[:] will do what you want, but graph.nodes[:] doesn't make since because you're asking for a slice of the function, rather than a slice of the list it returns. In later versions it is a NodeView. I think your code might work in this version. If not, then I'm fairly confident that graph.nodes()[:] still works.
[I don't have the newer version on the computer I'm using right now, so I'm not 100% sure.]
Thank you for everyone.I have used a method to know something.
I used print type(g)and print type(g.nodes)and found something like the picture.
the type of g and g.nodes
So this is created by Jython which combine python and Java and it is not an object of networkx.
Okay,now I can convince myself.
Thanks for everyone again!

python data structure for certain networks, breadth first search

The title may be little unclear, but to give a brief explanation, I'm applying some biological networks like protein networks to programming. I want to use a breadth-first search to calculate some values. Here's an example of a network I'm currently working with:
On a side note, just because a node isn't named doesn't mean its not a node. Just means its name is not significant for the network.
Simpler example:
My problem here is that I need to represent this network with a data structure, which I need to use to calculate 2 values for every node:
The # of signal paths for a node (how many paths there are from input to output that includes the node)
The # of feedback loops for a node (how many loop paths the node is in)
I need to calculate these values for every single node in the network. Python came to mind because it's a standard for bioinformatics, but I'm open to other languages with potentially built in structures. Within Python, the only thing that comes to mind is some form of DFA/dictionary sort of deal to represent these kind of networks, but I'm posting the question here to see if anyone else has a better idea.
NetworkX works well. If you read section 4.39.2 of the documentation, you will see how to do BFS with NetworkX

Uniform Cost Search: How do I retain the solution?

The algorithm can be found here: http://en.wikipedia.org/wiki/Uniform-cost_search
I am struggling with keeping the solution. I am writing in Python, and my implementation finds the optimal solution as expected, but I don't know how to store it.
I would imagine when exploring new neighbors, I need to add the current node to the new neighbors paths, however it isn't working how I expect.
It is a homework assignment so I won't post code yet, but if it is necessary for your help I can post snippets.
Thanks!
Are you looking for finding the final path? In that case you can just store the address of the parent node in each node and traverse upwards to root when you find the destination.

Categories

Resources