Depth-First Search and Breadth-First Search in Python · Edd Mann - MywallpapersMobi

Depth-First Search and Breadth-First Search in Python · Edd Mann

Skip to content


  • All gists
  • GitHub
Sign up for a GitHub account
Sign in

Instantly share code, notes, and snippets.


  • Star

    1


  • Fork

    1

@avrilcoghlan
/ tree_traversal.py

Last active Apr 4, 2018

Embed
What would you like to do?


Learn more about clone URLs



Download ZIP

Python script for depth-first search and breadth-first search of a simple tree

Raw




tree_traversal.py

def DFS_dist_from_node(query_node, parents):
"""Return dictionary containing distances of parent GO nodes from the query"""
result = {}
stack = []
stack.append( (query_node, 0) )
while len(stack) > 0:
print("stack=", stack)
node, dist = stack.pop()
result[node] = dist
if node in parents:
for parent in parents[node]:
# Get the first member of each tuple, see
# http://stackoverflow.com/questions/12142133/how-to-get-first-element-in-a-list-of-tuples
stack_members = [x[0] for x in stack]
if parent not in stack_members:
stack.append( (parent, dist+1) )
return result
def BFS_dist_from_node(query_node, parents):
"""Return dictionary containing minimum distances of parent GO nodes from the query"""
result = {}
queue = []
queue.append( (query_node, 0) )
while queue:
print("queue=", queue)
node, dist = queue.pop(0)
result[node] = dist
if node in parents: # If the node *has* parents
for parent in parents[node]:
# Get the first member of each tuple, see
# http://stackoverflow.com/questions/12142133/how-to-get-first-element-in-a-list-of-tuples
queue_members = [x[0] for x in queue]
if parent not in result and parent not in queue_members: # Don’t visit a second time
queue.append( (parent, dist+1) )
return result
if __name__ == "__main__":
parents = dict()
parents = N1: [N2, N3, N4], N3: [N6, N7], N4: [N3], N5: [N4, N8], N6: [N13],
N8: [N9], N9: [N11], N10: [N7, N9], N11: [N14], N12: [N5]
print("Depth-first search:")
dist = DFS_dist_from_node(N1, parents)
print(dist)
print("Breadth-first search:")
dist = BFS_dist_from_node(N1, parents)
print(dist)

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

  • © 2018 GitHub, Inc.
  • Terms
  • Privacy
  • Security
  • Status
  • Help

  • Contact GitHub
  • Pricing
  • API
  • Training
  • Blog
  • About


You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session.
You signed out in another tab or window. Reload to refresh your session.

Press h to open a hovercard with more details.

 
Problem Solving with Algorithms and Data Structures

    • Runestone in social media:

      Follow @iRunestone

    • Help support us:
    • Change Course
    • Instructor’s Page
    • Progress Page
    • Edit Profile
    • Change Password
    • Register
    • Login
    • Navigation Help
    • Help for Instructors
    • About Runestone
    • Report A Problem
  • Chapters

      • 1. Introduction
      • 2. Analysis
      • 3. Basic Data Structures
      • 4. Recursion
      • 5. Sorting and Searching
      • 6. Trees and Tree Algorithms
      • 7. Graphs and Graph Algorithms

7.9. Implementing Breadth First Search ¶

With the graph constructed we can now turn our attention to the
algorithm we will use to find the shortest solution to the word ladder
problem. The graph algorithm we are going to use is called the “breadth
first search” algorithm. Breadth first search (BFS) is one of
the easiest algorithms for searching a graph. It also serves as a
prototype for several other important graph algorithms that we will
study later.

Given a graph \(G\) and a starting vertex \(s\), a breadth
first search proceeds by exploring edges in the graph to find all the
vertices in \(G\) for which there is a path from \(s\). The
remarkable thing about a breadth first search is that it finds all the
vertices that are a distance \(k\) from \(s\) before it
finds any vertices that are a distance \(k+1\). One good way to
visualize what the breadth first search algorithm does is to imagine
that it is building a tree, one level of the tree at a time. A breadth
first search adds all children of the starting vertex before it begins
to discover any of the grandchildren.

To keep track of its progress, BFS colors each of the vertices white,
gray, or black. All the vertices are initialized to white when they are
constructed. A white vertex is an undiscovered vertex. When a vertex is
initially discovered it is colored gray, and when BFS has completely
explored a vertex it is colored black. This means that once a vertex is
colored black, it has no white vertices adjacent to it. A gray node, on
the other hand, may have some white vertices adjacent to it, indicating
that there are still additional vertices to explore.

The breadth first search algorithm shown in Listing 2 below uses the
adjacency list graph representation we developed earlier. In addition it uses a Queue,
a crucial point as we will see, to decide which vertex to explore next.

In addition the BFS algorithm uses an extended version of the Vertex
class. This new vertex class adds three new instance variables:
distance, predecessor, and color. Each of these instance variables also
has the appropriate getter and setter methods. The code for this
expanded Vertex class is included in the pythonds package, but we
will not show it to you here as there is nothing new to learn by seeing
the additional instance variables.

BFS begins at the starting vertex s and colors start gray to
show that it is currently being explored. Two other values, the distance
and the predecessor, are initialized to 0 and None respectively for
the starting vertex. Finally, start is placed on a Queue. The
next step is to begin to systematically explore vertices at the front of
the queue. We explore each new node at the front of the queue by
iterating over its adjacency list. As each node on the adjacency list is
examined its color is checked. If it is white, the vertex is unexplored,
and four things happen:

  1. The new, unexplored vertex nbr, is colored gray.
  2. The predecessor of nbr is set to the current node currentVert
  3. The distance to nbr is set to the distance to currentVert + 1
  4. nbr is added to the end of a queue. Adding nbr to the end of
    the queue effectively schedules this node for further exploration,
    but not until all the other vertices on the adjacency list of
    currentVert have been explored.

Listing 2

from pythonds.graphs import Graph, Vertexfrom pythonds.basic import Queuedef bfs(g,start): start.setDistance(0) start.setPred(None) vertQueue = Queue() vertQueue.enqueue(start) while (vertQueue.size() > 0): currentVert = vertQueue.dequeue() for nbr in currentVert.getConnections(): if (nbr.getColor() == 'white'): nbr.setColor('gray') nbr.setDistance(currentVert.getDistance() + 1) nbr.setPred(currentVert) vertQueue.enqueue(nbr) currentVert.setColor('black')

Let’s look at how the bfs function would construct the breadth first
tree corresponding to the graph in Figure 1 . Starting
from fool we take all nodes that are adjacent to fool and add them to
the tree. The adjacent nodes include pool, foil, foul, and cool. Each of
these nodes are added to the queue of new nodes to expand.
Figure 3 shows the state of the in-progress tree along with the
queue after this step.

../_images/bfs1.png

Figure 3: The First Step in the Breadth First Search

In the next step bfs removes the next node (pool) from the front of
the queue and repeats the process for all of its adjacent nodes.
However, when bfs examines the node cool, it finds that the color of
cool has already been changed to gray. This indicates that there is a
shorter path to cool and that cool is already on the queue for further
expansion. The only new node added to the queue while examining pool is
poll. The new state of the tree and queue is shown in Figure 4 .

../_images/bfs2.png

Figure 4: The Second Step in the Breadth First Search

The next vertex on the queue is foil. The only new node that foil can
add to the tree is fail. As bfs continues to process the queue,
neither of the next two nodes add anything new to the queue or the tree.
Figure 5 shows the tree and the queue after expanding all the
vertices on the second level of the tree.

../_images/bfs3.png

Figure 5: Breadth First Search Tree After Completing One Level

../_images/bfsDone.png

FIgure 6: Final Breadth First Search Tree

You should continue to work through the algorithm on your own so that
you are comfortable with how it works. Figure 6 shows the
final breadth first search tree after all the vertices in
Figure 3 have been expanded. The amazing thing about the
breadth first search solution is that we have not only solved the
FOOL–SAGE problem we started out with, but we have solved many other
problems along the way. We can start at any vertex in the breadth first
search tree and follow the predecessor arrows back to the root to find
the shortest word ladder from any word back to fool. The function below ( Listing 3 ) shows how to follow the predecessor links to
print out the word ladder.

Listing 3

def traverse(y): x = y while (x.getPred()): print(x.getId()) x = x.getPred() print(x.getId())traverse(g.getVertex('sage'))
  • Next Section – 7.10. Breadth First Search Analysis

    Programiz Logo

    search

    close

    Simplest programming tutorials for beginners

    Breadth first search

    Traversal means visiting all the nodes of a graph. Breadth first traversal or Breadth first Search is a recursive algorithm for searching all the vertices of a graph or tree data structure. In this article, you will learn with the help of examples the BFS algorithm, BFS pseudocode and the code of the breadth first search algorithm with implementation in C++, C, Java and Python programs.

    BFS algorithm

    A standard DFS implementation puts each vertex of the graph into one of two categories:

    1. Visited
    2. Not Visited

    The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

    The algorithm works as follows:

    1. Start by putting any one of the graph’s vertices at the back of a queue.
    2. Take the front item of the queue and add it to the visited list.
    3. Create a list of that vertex’s adjacent nodes. Add the ones which aren’t in the visited list to the back of the queue.
    4. Keep repeating steps 2 and 3 until the queue is empty.

    The graph might have two different disconnected parts so to make sure that we cover every vertex, we can also run the BFS algorithm on every node

    BFS example

    Let’s see how the Breadth First Search algorithm works with an example. We use an undirected graph with 5 vertices.

    undirected graph with 5 vertices

    We start from vertex 0, the BFS algorithm starts by putting it in the Visited list and putting all its adjacent vertices in the stack.

    visit start vertex and add its adjacent vertices to queue

    Next, we visit the element at the front of queue i.e. 1 and go to its adjacent nodes. Since 0 has already been visited, we visit 2 instead.

    visit the first neighbour of start node 0, which is 1

    Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the back of the queue and visit 3, which is at the front of the queue.

    visit 2 which was added to queue earlier to add its neighbours

    visit

     

    Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already visited. We visit it.

    visit last remaining item in stack to check if it has unvisited neighbours

    Since the queue is empty, we have completed the Depth First Traversal of the graph.

    BFS pseudocode

    create a queue Q
    mark v as visited and put v into Q
    while Q is non-empty remove the head u of Q mark and enqueue all (unvisited) neighbours of u

    BFS code

    The code for the Breadth First Search Algorithm with an example is shown below. The code has been simplified so that we can focus on the algorithm rather than other details.

    BFS in C

    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 40
    struct queue int items[SIZE]; int front; int rear;
    ;
    struct queue* createQueue();
    void enqueue(struct queue* q, int);
    int dequeue(struct queue* q);
    void display(struct queue* q);
    int isEmpty(struct queue* q);
    void printQueue(struct queue* q);
    struct node int vertex; struct node* next;
    ;
    struct node* createNode(int);
    struct Graph int numVertices; struct node** adjLists; int* visited;
    ;
    struct Graph* createGraph(int vertices);
    void addEdge(struct Graph* graph, int src, int dest);
    void printGraph(struct Graph* graph);
    void bfs(struct Graph* graph, int startVertex);
    int main() struct Graph* graph = createGraph(6); addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 2); addEdge(graph, 1, 4); addEdge(graph, 1, 3); addEdge(graph, 2, 4); addEdge(graph, 3, 4); bfs(graph, 0); return 0;
    void bfs(struct Graph* graph, int startVertex) struct queue* q = createQueue(); graph->visited[startVertex] = 1; enqueue(q, startVertex); while(!isEmpty(q)) printQueue(q); int currentVertex = dequeue(q); printf("Visited %d\n", currentVertex); struct node* temp = graph->adjLists[currentVertex]; while(temp) int adjVertex = temp->vertex; if(graph->visited[adjVertex] == 0) graph->visited[adjVertex] = 1; enqueue(q, adjVertex); temp = temp->next;
    struct node* createNode(int v) struct node* newNode = malloc(sizeof(struct node)); newNode->vertex = v; newNode->next = NULL; return newNode;
    struct Graph* createGraph(int vertices) struct Graph* graph = malloc(sizeof(struct Graph)); graph->numVertices = vertices; graph->adjLists = malloc(vertices * sizeof(struct node*)); graph->visited = malloc(vertices * sizeof(int)); int i; for (i = 0; i < vertices; i++) graph->adjLists[i] = NULL; graph->visited[i] = 0; return graph;
    void addEdge(struct Graph* graph, int src, int dest) // Add edge from src to dest struct node* newNode = createNode(dest); newNode->next = graph->adjLists[src]; graph->adjLists[src] = newNode; // Add edge from dest to src newNode = createNode(src); newNode->next = graph->adjLists[dest]; graph->adjLists[dest] = newNode;
    struct queue* createQueue() struct queue* q = malloc(sizeof(struct queue)); q->front = -1; q->rear = -1; return q;
    int isEmpty(struct queue* q) if(q->rear == -1) return 1; else return 0;
    void enqueue(struct queue* q, int value) if(q->rear == SIZE-1) printf("\nQueue is Full!!"); else if(q->front == -1) q->front = 0; q->rear++; q->items[q->rear] = value;
    int dequeue(struct queue* q) int item; if(isEmpty(q)) printf("Queue is empty"); item = -1; else item = q->items[q->front]; q->front++; if(q->front > q->rear) printf("Resetting queue"); q->front = q->rear = -1; return item;
    void printQueue(struct queue *q) int i = q->front; if(isEmpty(q)) printf("Queue is empty"); else printf("\nQueue contains \n"); for(i = q->front; i < q->rear + 1; i++) printf("%d ", q->items[i]); 

    Breadth First Search in C++

    #include <iostream>
    #include <list>
    using namespace std;
    class Graph int numVertices; list *adjLists; bool* visited;
    public: Graph(int vertices); void addEdge(int src, int dest); void BFS(int startVertex);
    ;
    Graph::Graph(int vertices) numVertices = vertices; adjLists = new list[vertices];
    void Graph::addEdge(int src, int dest) adjLists[src].push_back(dest); adjLists[dest].push_back(src);
    void Graph::BFS(int startVertex) visited = new bool[numVertices]; for(int i = 0; i < numVertices; i++) visited[i] = false; list queue; visited[startVertex] = true; queue.push_back(startVertex); list::iterator i; while(!queue.empty()) int currVertex = queue.front(); cout << "Visited " << currVertex << " "; queue.pop_front(); for(i = adjLists[currVertex].begin(); i != adjLists[currVertex].end(); ++i) int adjVertex = *i; if(!visited[adjVertex]) visited[adjVertex] = true; queue.push_back(adjVertex);
    int main() Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); g.BFS(2); return 0;

    BFS Java code

    import java.io.*;
    import java.util.*;
    class Graph private int numVertices; private LinkedList<Integer> adjLists[]; private boolean visited[]; Graph(int v) numVertices = v; visited = new boolean[numVertices]; adjLists = new LinkedList[numVertices]; for (int i=0; i i = adjLists[currVertex].listIterator(); while (i.hasNext()) int adjVertex = i.next(); if (!visited[adjVertex]) visited[adjVertex] = true; queue.add(adjVertex); public static void main(String args[]) Graph g = new Graph(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); System.out.println("Following is Breadth First Traversal "+ "(starting from vertex 2)"); g.BFS(2);
    }

    BFS in Python

    import collections
    def bfs(graph, root): visited, queue = set(), collections.deque([root]) visited.add(root) while queue: vertex = queue.popleft() for neighbour in graph[vertex]: if neighbour not in visited: visited.add(neighbour) queue.append(neighbour)
    if __name__ == '__main__': graph = 0: [1, 2], 1: [2], 2: [3], 3: [1,2] breadth_first_search(graph, 0)

    Data Structure & Algorithms

    Bubble Sort Algorithm
    Insertion Sort Algorithm
    Selection Sort Algorithm
    Heap Sort Algorithm
    Merge Sort Algorithm
    Stack
    Queue
    Circular Queue
    Linked List
    Types of Linked List – Singly linked, doubly linked and circular
    Linked List Operations
    Tree Data Structure
    Tree Traversal – inorder, preorder and postorder
    Binary Search Tree(BST)
    Graph Data Stucture
    DFS algorithm
    Adjacency List
    Adjacency Matrix
    Breadth first search
    Kruskals Algorithm
    Prims Algorithm
    Dynamic Programming
    Dijkstras Algorithm
    Bellman Fords Algorithm

    close

    It looks like you are using Adblock!

    It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:

    Disabling AdBlock on Programiz. We do not use intrusive ads.

    or

    Donate on Paypal

    By using Programiz, you agree to use cookies as stated in our Privacy policy Continue