Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
55 views7 pages

AI Workshop: BFS & DFS Coding

The document contains source code for implementing breadth-first search (BFS) and depth-first search (DFS) on three different graphs. For each graph, it provides the graph representation as a dictionary, the BFS and DFS algorithms, and sample outputs. The algorithms take the graph, a visited list/set, and a starting node as inputs and traverse the graph, printing the nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views7 pages

AI Workshop: BFS & DFS Coding

The document contains source code for implementing breadth-first search (BFS) and depth-first search (DFS) on three different graphs. For each graph, it provides the graph representation as a dictionary, the BFS and DFS algorithms, and sample outputs. The algorithms take the graph, a visited list/set, and a starting node as inputs and traverse the graph, printing the nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TUGAS

WORKSHOP KECERDASAN BUATAN

3120521002
DEATRI NARI RATIH
D3 TEKNIK INFORMATIKA PSDKU-LA
POLITEKNIKK ELEKTRONIKA NEGERI SURABAYA
1. Ubahlah program di atas untuk Graph di bawah ini:

 Breadth First Search

Source code :
graph = {
    'A' : ['B', 'C'],
    'B' : ['H', 'I'],
    'C' : ['D', 'E'],
    'D' : ['F', 'G'],
    'E' : ['I'],
    'F' : ['H'],
    'G' : ['I'],
    'H' : ['I'],
    'I' : []
}

visited = [] # List to keep track of visited nodes.


queue = []     #Initialize a queue

def bfs(visited, graph, node):


  visited.append(node)
  queue.append(node)

  while queue:
    s = queue.pop(0)
    print (s, end = " ")

    for neighbour in graph[s]:


      if neighbour not in visited:
        visited.append(neighbour)
        queue.append(neighbour)

# Driver Code
bfs(visited, graph, 'A')

Output :

 Depth First Search

Source Code :
graph = {
'A' : ['B', 'C'],
'B' : ['H', 'I'],
'C' : ['D', 'E'],
'D' : ['F', 'G'],
'E' : ['I'],
'F' : ['H'],
'G' : ['I'],
'H' : ['I'],
'I' : []
}
visited = set()
def dfs(visited, graph, node):
    if node not in visited:
        print (node , end = " ")
        visited.add(node)
        for neighbour in graph[node]:
            dfs(visited, graph, neighbour)

dfs(visited, graph, 'A')

Output :
2. Ubahlah program di atas untuk Graph di bawah ini:

 Breadth First Search


Source Code :
graph = {
1 : [2, 3],
2 : [4, 5],
3 : [4, 6],
4 : [5, 6, 8, 7],
5 : [7],
6 : [8, 9],
7 : [9],
8 : [9],
9 : []
}

visited = []
queue = []

def bfs(visited, graph, node):


 visited.append(node)
 queue.append(node)
 while queue:
    m = queue.pop(0)
    print (m, end = " ")
  
    for neighbour in graph[m]:
     if neighbour not in visited:
      visited.append(neighbour)
      queue.append(neighbour)

print("Following is the Breadth-First Search")


bfs(visited, graph, 1)
Output :

 Depth Search First

Source code :
graph = {
1 : [2, 3],
2 : [5, 4],
3 : [4, 6],
4 : [5, 6, 8, 7],
5 : [7],
6 : [8, 9],
7 : [9],
8 : [9],
9 : []
}

visited = set()

def dfs(visited, graph, node):


    if node not in visited:
        print (node , end = " ")
        visited.add(node)
        for neighbour in graph[node]:
            dfs(visited, graph, neighbour)

dfs(visited, graph, 1)

Output :
3. Ubahlah program di atas untuk Graph di bawah ini:

 Breadth First Search


Source Code :
graph = {
0 : [3, 4],
1 : [0, 6],
2 : [6, 5],
3 : [1,7],
4 : [6],
5 : [6, 7],
6 : [2, 4],
7 : [5]
}

visited = []
queue = []

def bfs(visited, graph, node):


  visited.append(node)
  queue.append(node)
 
  while queue:
    m = queue.pop(0)
    print (m, end = " ")
  
    for neighbour in graph[m]:
     if neighbour not in visited:
       visited.append(neighbour)
       queue.append(neighbour)
    
print("Following is the Breadth-First Search")
bfs(visited, graph, 0)
Output :

 Depth First Search


Source code :
graph = {
0 : [3, 4],
1 : [0, 6],
2 : [6, 5],
3 : [1,7],
4 : [6],
5 : [6, 7],
6 : [2, 4],
7 : [5]
}

visited = set()

def dfs(visited, graph, node):


    if node not in visited:
       print (node , end = " ")
       visited.add(node)
       for neighbour in graph[node]:
           dfs(visited, graph, neighbour)

dfs(visited, graph, 0)

Output :

You might also like