0% found this document useful (0 votes)
18 views14 pages

MD - Shawkat Hossain - 20CSE046

The document discusses various graph algorithms including breadth-first search, depth-first search, Dijkstra's algorithm, graph coloring using backtracking, forward checking and arc consistency. Code implementations are provided for these algorithms in Python.

Uploaded by

zahidul.cse7.bu
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)
18 views14 pages

MD - Shawkat Hossain - 20CSE046

The document discusses various graph algorithms including breadth-first search, depth-first search, Dijkstra's algorithm, graph coloring using backtracking, forward checking and arc consistency. Code implementations are provided for these algorithms in Python.

Uploaded by

zahidul.cse7.bu
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/ 14

This is the map to go my home from barishal.

Now i apply Breath First Search(BFS) algorithm on this graph:


Here source node is Notullabad .

Traversal order => Notullabad, barishal sadar, Rupatoli, Lohar pool, Nolcity ,Barishal
University , Jhalokati , Zero point, baghri bazar, medical mor, bypass mor, shohag clinic, Bari.

Simulation of BFS Algorithm:


Now i apply Depth First Search(DFS) algorithm on this graph:
Here source node is Notullabad .

Traversal order => Notullabad, barishal sadar, Rupatoli, Lohar pool , Nolcity, Barishal University,
Zero point, Jhalokati, baghri bazar, medical mor ,shohag clinic, bypass mor, Bari.

Simulation of DFS Algorithm:

Now Performing Dijkstra Algorithm To find shortest path from Notullabad to Bari:
Here source is Notullabad and destination is Bari .Cost for shortest path travel is 19.

Shortest path to travel Notullabad to Bari is .

Notullabad⇒Loharpool⇒Rupatoli⇒BarishalUniversity⇒Nolcity⇒Zeropoint⇒Jhalokati⇒

baghri bazar⇒medical mor⇒shohag clinic⇒Bari


Dijkstra in Python :
import sys

class Graph():

def __init__(self, vertices):

self.V = vertices

self.graph = [[0 for column in range(vertices)]

for row in range(vertices)]

def printSolution(self, dist):

print("Vertex \tDistance from Source")

for node in range(self.V):

print(node, "\t", dist[node])

def minDistance(self, dist, sptSet):

min = sys.maxsize

for u in range(self.V):

if dist[u] < min and sptSet[u] == False:

min = dist[u]
min_index = u

return min_index

def dijkstra(self, src):

dist = [sys.maxsize] * self.V

dist[src] = 0

sptSet = [False] * self.V

for cout in range(self.V):

x = self.minDistance(dist, sptSet)

sptSet[x] = True

for y in range(self.V):

if self.graph[x][y] > 0 and sptSet[y] == False and \

dist[y] > dist[x] + self.graph[x][y]:

dist[y] = dist[x] + self.graph[x][y]

self.printSolution(dist)

if __name__ == "__main__":

g = Graph(13)

g.graph = [[0, 2, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[2, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 4, 0, 3, 6, 3, 0, 10, 0, 0, 0, 0, 0],

[1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 6, 0, 0, 1, 1, 4, 0, 0, 0, 0, 0],

[0, 0, 3, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 1, 5, 0, 2, 0, 0, 0, 0, 0],

[0, 0, 10, 0, 4, 0, 2, 0, 2, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 4, 0, 0, ],

[0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 1, 4],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 2],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 0]

g.dijkstra(0)
Graph coloring using Backtracking :

Here i replace node with integer value,


Code:
import numpy as np

import networkx as nx

import matplotlib.pyplot as plt

adj_matrix = np.array([

[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]

])

G = nx.from_numpy_matrix(adj_matrix)

def is_safe(node, color, colors):

for neighbor in G.neighbors(node):

if colors[neighbor] == color:

return False
return True

def color_graph(node, colors, num_colors):

if node == len(G.nodes()):

return True

for color in range(1, num_colors + 1):

if is_safe(node, color, colors):

colors[node] = color

if color_graph(node + 1, colors, num_colors):

return True

colors[node] = 0

return False

num_colors = 3 # Choose the number of colors

colors = [0] * len(G.nodes())

if color_graph(0, colors, num_colors):

color_map = {1: 'red', 2: 'green', 3: 'blue'} # Color mapping

node_colors = [color_map[colors[node]] for node in G.nodes()]

nx.draw(G, with_labels=True, node_color=node_colors)

plt.title("Graph Coloring using Backtracking")

plt.show()

else:

print("The graph cannot be colored with the given number of colors.")


Early detection of failure: Forward checking :

Code:
import numpy as np

import networkx as nx

import matplotlib.pyplot as plt

adj_matrix = np.array([

[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]

])

G = nx.from_numpy_matrix(adj_matrix)

def is_safe(node, color, colors):

for neighbor in G.neighbors(node):

if colors[neighbor] == color:

return False

return True

def color_graph(node, colors, num_colors, remaining_colors):

if node == len(G.nodes()):

return True

for color in remaining_colors[node]:

if is_safe(node, color, colors):


colors[node] = color

new_remaining_colors = [set(remaining_colors[i]) for i in range(len(G.nodes()))]

for neighbor in G.neighbors(node):

new_remaining_colors[neighbor].discard(color)

if color_graph(node + 1, colors, num_colors, new_remaining_colors):

return True

colors[node] = 0

return False

num_colors = 3

colors = [0] * len(G.nodes())

remaining_colors = [set(range(1, num_colors + 1)) for _ in range(len(G.nodes()))]

if color_graph(0, colors, num_colors, remaining_colors):

color_map = {1: 'red', 2: 'green', 3: 'blue'}

node_colors = [color_map[colors[node]] for node in G.nodes()]

nx.draw(G, with_labels=True, node_color=node_colors)

plt.title("Graph Coloring using Backtracking with Forward Checking")

plt.show()

else:

print("The graph cannot be colored with the given number of colors.")

Arc consistency algorithm AC-3:


import numpy as np

import networkx as nx

import matplotlib.pyplot as plt

from collections import deque

adj_matrix = np.array([

[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],

[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]

])

G = nx.from_numpy_matrix(adj_matrix)

def ac3(queue, domains):

while queue:

(i, j) = queue.pop()

if revise(domains, i, j):
if len(domains[i]) == 0:

return False

for k in G.neighbors(i):

if k != j:

queue.append((k, i))

return True

def revise(domains, i, j):

revised = False

for x in domains[i].copy():

if not any(adj_matrix[x][y] for y in domains[j]):

domains[i].remove(x)

revised = True

return revised

def color_graph(node, colors, num_colors, domains):

if node == len(G.nodes()):

return True

for color in domains[node]:

colors[node] = color

if ac3([(neighbor, node) for neighbor in G.neighbors(node)], domains):

if color_graph(node + 1, colors, num_colors, domains):

return True

colors[node] = 0
return False

num_colors = 3

colors = [0] * len(G.nodes())

domains = [set(range(1, num_colors + 1)) for _ in range(len(G.nodes()))]

if color_graph(0, colors, num_colors, domains):

color_map = {1: 'red', 2: 'green', 3: 'blue'}

node_colors = [color_map[colors[node]] for node in G.nodes()]

nx.draw(G, with_labels=True, node_color=node_colors)

plt.title("Graph Coloring using Backtracking with AC-3")

plt.show()

else:

print("The graph cannot be colored with the given number of colors.")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy