Skip to main content

Graph Search Algorithms (Python Code)

 Graph Algorithm Code for YT Videos


Detect Cycle in Graph

Code Explanation :

  • For UnDirected Graph


adj_list = {
    "A" : ["C", "B", "D"],
    "B" : ["A", "D"],
    "C" : ["A"],
    "D" : ["A","B" "E"],
    "E" : ["D"]
}

color = {}
parent = {}

for u in adj_list.keys():
    color[u] = 'W'
    parent[u] = None

def dfs(u, color, parent):
    color[u] = 'G'
    for v in adj_list[u]:
        if color[v] == 'W':
            parent[v] = u
            cycle = dfs(v, color, parent)
            if cycle == True:
                return True
        elif color[v] == "G" and parent[u]!=v:
            print ("Cycle found", u, v)
            return True
    color[u] = "B"
    return False

is_cyclic = False
for u in adj_list.keys():
    if color[u] == 'W':
        is_cyclic = dfs(u, color, parent)
        if is_cyclic == True:
            break
print ("Is_cyclic ", is_cyclic)

For Directed Graph



adj_list = {
    "A" : ["C", "B"],
    "B" : ["D"],
    "C" : [],
    "D" : ["A", "E"],
    "E" : []
}

color = {}
parent = {}

for u in adj_list.keys():
    color[u] = 'W'
    parent[u] = None
              
def dfs(u, color):
    color[u] = 'G'
    print (u)
    for v in adj_list[u]:
        if color[v]=='W':
            cycle = dfs(v, color)
            if cycle:
                return True
        elif color[v]=='G': # cycle is present
            print (u, v)
            return True
    color[u] = 'B'
    return False

is_cyclic = False
for u in adj_list.keys():
    if color[u] == 'W':
        is_cyclic = dfs(u, color)
        if is_cyclic:
            break
print ("Is cyclic " ,is_cyclic)
>> True

Comments

Popular posts from this blog

Transparent Image overlay(Alpha blending) with OpenCV and Python

(a)Final Blended Image                     (b) Background Image                             (c)Foreground Image                               Alpha blending Alpha blending is the process of overlaying a foreground image with transparency over a background Image. The transparent image is generally a PNG image.It consists of four channels (RGBA).The fourth channel is the alpha channel which holds the transparency magnitude. Image (b) is a background image and image (c) is the foreground / overlay image. Image (a) is the final blended image obtained by blending  the overalay image using the alpha mask.  Below is the image(Fig d) of the alpha channel of the overlay  image. (d).Alpha Channel At every pixel of the image, we blend the background and foreground image color(F) and background color (B) using the alpha mask . At every pixel value of alpha lie in range(0,255), a pixel intensity of 0 means black color and pixel instensity of 255 means whit

Fast Pixel Processing with OpenCV and Python

In this post. I will explain how fast pixel manipulation of an image can be done in Python and OpenCV. Image processing is a CPU intensive task. It involves processing on large arrays. Hence when you are implementing your Image Processing algorithm, you algorithm needs to be highly efficient. The type of operation that can be applied on an Image can be classified into three categories.