Teaching Kids Programming – A Light Talk on Breadth First Search, Uniform Cost Search and Dijkstra Shortest Path Graph Algorithms


Teaching Kids Programming: Videos on Data Structures and Algorithms

Breadth First Search Algorithm (BFS) performs a level by level order, and we can use this algorithm to get the shortest path in a unweighted graph. The BFS Algorithm uses a Queue which is a First In First Out Data Structure.

The BFS code works like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
def BFS(G, s, e):
    if s == e:
        return 0
    q = deque([(0, s)])
    seen = set()
    while q:
        c, v = q.popleft()
        if v in seen:
            return c
        seen.add(v)
        for x in G[v]:
            q.append((c + 1, x))
    return inf
def BFS(G, s, e):
    if s == e:
        return 0
    q = deque([(0, s)])
    seen = set()
    while q:
        c, v = q.popleft()
        if v in seen:
            return c
        seen.add(v)
        for x in G[v]:
            q.append((c + 1, x))
    return inf

We use a hash set to remember the vertices that have been visited. For Breadth First Search Algorithm, this check can be done also when the elements are about to be enqueud like this:

1
2
3
4
5
6
7
8
9
10
11
12
def BFS(G, s, e):
    if s == e:
        return 0
    q = deque([(0, s)])
    seen = set()
    while q:
        c, v = q.popleft()
        for x in G[v]:
            if x not in seen:    
                seen.append(x)
                q.append((c + 1, x))
    return inf
def BFS(G, s, e):
    if s == e:
        return 0
    q = deque([(0, s)])
    seen = set()
    while q:
        c, v = q.popleft()
        for x in G[v]:
            if x not in seen:    
                seen.append(x)
                q.append((c + 1, x))
    return inf

The time complexity is tex_7c1af5ff2547ecbdbb71bdcf2a3d775b Teaching Kids Programming - A Light Talk on Breadth First Search, Uniform Cost Search and Dijkstra Shortest Path Graph Algorithms algorithms Breadth First Search Dijkstra Shortest Path Graph Algorithm python teaching kids programming Uniform Cost Search youtube video because in the worst case, all edges and vertices in the (unweighted) graph (or graph with equal weights) are explorered.

The Uniform Cost Search (UCS) Algorithm is a variant of Dijkstra.

We can just change the queue (FIFO) to priority queue (or heap) on the basis of the first BFS and that is basically UCS:

1
2
3
4
5
6
7
8
9
10
11
12
13
def UCS(G, s, e):
    if s == e:
        return 0
    q = [(0, s)]
    seen = set()
    while q:
        c, v = heappop(q)
        if v in seen:
            return c
        seen.add(v)
        for x, w in G[v]:
            heappush(q, (c + w, x))
    return inf
def UCS(G, s, e):
    if s == e:
        return 0
    q = [(0, s)]
    seen = set()
    while q:
        c, v = heappop(q)
        if v in seen:
            return c
        seen.add(v)
        for x, w in G[v]:
            heappush(q, (c + w, x))
    return inf

Unlike BFS, UCS can be used on weighted graph. The above G is a adjacent list graph representation.

The Dijkstra algorithm adds step of “Updating the Estimates” and thus keeps a one-dimensional array e.g. d that stores the shortest distance from the source. The Dijkstra algorithm is complete as it computes all shortest path from the source while the BFS and UCS are not complete aka they only return the shortest path between a pair of vertice.

Both Dijkstra and UCS runs at time complexity tex_8bbedbf8fd07930da323e2ffab149714 Teaching Kids Programming - A Light Talk on Breadth First Search, Uniform Cost Search and Dijkstra Shortest Path Graph Algorithms algorithms Breadth First Search Dijkstra Shortest Path Graph Algorithm python teaching kids programming Uniform Cost Search youtube video

Shortest Path Algorithms

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
1054 words
Last Post: Teaching Kids Programming - Introduction to Dijkstra Single Source Shortest Path Graph Algorithm
Next Post: Teaching Kids Programming - Depth First/Limit Search and Iterative Deepening Search Algorithm on Unweighted Graph

The Permanent URL is: Teaching Kids Programming – A Light Talk on Breadth First Search, Uniform Cost Search and Dijkstra Shortest Path Graph Algorithms

Leave a Reply