You are on page 1of 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