dfs and bfs in python

Solutions on MaxInterview for dfs and bfs in python by the best coders in the world

showing results for - "dfs and bfs in python"
Jarrett
25 Jan 2018
1#include<stdio.h>
2#include<stdlib.h>
3 
4#define MAX 100  
5 
6#define initial 1
7#define waiting 2
8#define visited 3
9 
10int n;    
11int adj[MAX][MAX];
12int state[MAX]; 
13void create_graph();
14void BF_Traversal();
15void BFS(int v);
16 
17int queue[MAX], front = -1,rear = -1;
18void insert_queue(int vertex);
19int delete_queue();
20int isEmpty_queue();
21 
22int main()
23{
24	create_graph();
25	BF_Traversal();
26	return 0;
27}
28 
29void BF_Traversal()
30{
31	int v;
32	
33	for(v=0; v<n; v++) 
34		state[v] = initial;
35	
36	printf("Enter Start Vertex for BFS: \n");
37	scanf("%d", &v);
38	BFS(v);
39}
40 
41void BFS(int v)
42{
43	int i;
44	
45	insert_queue(v);
46	state[v] = waiting;
47	
48	while(!isEmpty_queue())
49	{
50		v = delete_queue( );
51		printf("%d ",v);
52		state[v] = visited;
53		
54		for(i=0; i<n; i++)
55		{
56			if(adj[v][i] == 1 && state[i] == initial) 
57			{
58				insert_queue(i);
59				state[i] = waiting;
60			}
61		}
62	}
63	printf("\n");
64}
65 
66void insert_queue(int vertex)
67{
68	if(rear == MAX-1)
69		printf("Queue Overflow\n");
70	else
71	{
72		if(front == -1) 
73			front = 0;
74		rear = rear+1;
75		queue[rear] = vertex ;
76	}
77}
78 
79int isEmpty_queue()
80{
81	if(front == -1 || front > rear)
82		return 1;
83	else
84		return 0;
85}
86 
87int delete_queue()
88{
89	int delete_item;
90	if(front == -1 || front > rear)
91	{
92		printf("Queue Underflow\n");
93		exit(1);
94	}
95	
96	delete_item = queue[front];
97	front = front+1;
98	return delete_item;
99}
100 
101void create_graph()
102{
103	int count,max_edge,origin,destin;
104 
105	printf("Enter number of vertices : ");
106	scanf("%d",&n);
107	max_edge = n*(n-1);
108 
109	for(count=1; count<=max_edge; count++)
110	{
111		printf("Enter edge %d( -1 -1 to quit ) : ",count);
112		scanf("%d %d",&origin,&destin);
113 
114		if((origin == -1) && (destin == -1))
115			break;
116 
117		if(origin>=n || destin>=n || origin<0 || destin<0)
118		{
119			printf("Invalid edge!\n");
120			count--;
121		}
122		else
123		{
124			adj[origin][destin] = 1;
125		}
126	}
127}
128
Franco
27 Nov 2016
1def dfs_paths(graph, start, goal, path=None):
2    if path is None:
3        path = [start]
4    if start == goal:
5        yield path
6    for next in graph[start] - set(path):
7        yield from dfs_paths(graph, next, goal, path + [next])
8
9list(dfs_paths(graph, 'C', 'F')) # [['C', 'F'], ['C', 'A', 'B', 'E', 'F']]
10
Oskar
25 Apr 2020
1def bfs(graph, start):
2    visited, queue = set(), [start]
3    while queue:
4        vertex = queue.pop(0)
5        if vertex not in visited:
6            visited.add(vertex)
7            queue.extend(graph[vertex] - visited)
8    return visited
9
10bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'}
11
Francesco
17 Jun 2018
1def shortest_path(graph, start, goal):
2    try:
3        return next(bfs_paths(graph, start, goal))
4    except StopIteration:
5        return None
6
7shortest_path(graph, 'A', 'F') # ['A', 'C', 'F']
8
queries leading to this page
uses of bfs and dfsbfs dfs in pythonwhat is bfs and dfswhen we use bfs and dfsdfs vs bfs pythonwhen to use bfs 26 when to use dfsusing both dfs and bfsbfs program explanation using cbfs with depth pythonwhere we use bfs and dfs in best first searchbfs algorithm c codedfs and bfs in pythondefine bfs and dfsbfs graph in cbfs implementation in ccreate breadth first search tree pythonimplement bfs and dfs in cbfs algorithm in cdfs and bfs pythonbfs or dfsbfs and dfs in c programmingbfs dfs code in pythonwhere to use bfs and dfsbfs and dfs geeksforgeeksgraph in python bfsdfs and bfs explainedbfs in data structure c programbfs and dfs examplebfs code data structuresan implementation of bfs 2c dfs 2c and a 2a search in pythonbfs dfs c programc 2b 2b code of bfswrite a program to traverse a graph using breadth first search 28bfs 29bfs dfs algorithmswho has made dfs and bfs algorithmbfs c programbfs algorithm and program in c bfs and dfs orderbfs dfs questionsbfs cbfs on cbfs and dfs code without using built in function in pythonbfs in cbfs dfs explaineddfs and bfs algorithm 28bfs 29 python codedfs and bfsbfs 2c dfs algorithmsbfs implementation cbfs 2fdfs are example of 2abfs code in cexample of dfs and bfsfor what we use bfs and dfswhich is best bfs or dfsbfs and dfs code in pythondfs and bfs inbfs and dfs examplesbfs using cwhat are dfs and bfs 3fdfs and bfs useswhen to bfs and dfsbfs program in cbfs and dfs code in cbfs and dfs meaningis a 2a dfs or bfsbfs in graph in cdfs bfs pythonbfs dynamic programming in cbfs and dfs data structurebfs and dfs pythonbfs and dfs best explanationbfs dfs codeapplication of bfs and dfsbfs dfs algorithmbfs and dfs code in c 2b 2bbfs and dfs in data structuredfs vs bfswhat is dfs and bfsbfs and dfs algorithm in javadfs and bfs in cdfs and bfs abfs pseudocode pythonbfs dfs code implementation c 2b 2bdfs or bfsbfs in python in graphbfs and dfs program in cbfs and dfs data structuresbfs and dfs program in c 2b 2b with outputbfs and dfsc program for bfs and dfsbfs and dfs algorithm c 2b 2bbreadth first search in cbfs 2c dfsbfs in c with examplewhen to use dfs and bfsbfs 26 dfswhen to use bfs and when dfsbreadth first search cpython dfs 2c bfswhich one to use dfs or bfsbfs in pythonbfs vs dfs in aibfs dfs data structurewrite a program for bfs and dfsbfs algorithm cmcqs on bfs and dfsexample for dfs and bfswhen to use bfs or dfsalgorithm for bfs and dfsdfs and bfs exampleexample for bfs and dfsbfs and dfs explaineddfs 2c bfs python codebfs and dfs explained pythonexplain bfs and dfs with exampleimplementation of dfs and bfsbfs dfs program in cwhen to use bfs and when to use dfswhen to use bfs vs dfsbfs dfs a 2a graph breadth first search in cexplain bfs and dfs with examplesbfs in the cbfs dfs implementationbfs and dfs in cdfs and bfs implementationuse of dfs and bfsinsert function of bfs graph javabfs and dfs c 2b 2b codewhen to use dfs or bfsalgorithm of bfs and dfsbfs 2c dfs 2c dls which is done firstbfs and dfs algorithm in cwhen bfs and dfs are usewhen should we use dfs and bfsbfs e dfswho created dfs and bfs algorithmwrite a program to implement dfs and bfs in pythonalgorithm dfs and bfsbfs dfs examplesimplementation on bfs and dfsbfs dfsbfs graph in cbfs dfs 5cexplain bfs and dfsbfs and dfs operationshow to print in bfs c programmingbreadth first search c exemplebfs 26 dfs algorithmbfs and dfs algorithms in cbfs and dfs applicationpython bfs dfsdfs vs bfs algorithm pythonbfs traversal of graph c 2b 2bbfs and dfs of graph in cpython dfs and bfs 3a bfs and dfsc bfsbfs and dfs in c 2b 2bwhen to use bfs and dfsbfs vs dfsimplementation of bfs and dfs in cbfs in c language using adjacency listbfs and dfs algorithmwhen to use bfs when to use dfsbfs and dfs program why we use dfs and bfsuse of bfs and dfsbfs and dfs in cppbfs and dfs codebfs dfs examplebfs exampledfs and bfs with examplecode for bfs in cbreadth first search on the grahp using linked list bfs and dfs techniques bfs and dfs gfggraph bfs in pythonbfs for graphs in cbfs and dfs definitionbfs in data structure in cbfs and dfs algorithm codescopebfs and dfsbfs and dfs in pythonwhat bfs and dfsbfs search algorithm in cdfs and bfs program in cbfs and dfs commonwhat is bfs or dfsdfs and bfs in python