Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
20 January 2021

Single-Source Shortest Path

by mervyn

Dijkstra Algorithm

Like Prim’s algorithm, we initially don’t know the distance to any vertex except the initial vertex

Iterate \(|V|\) times:

Example

Find the shortest distance from (K) to every other vertices

krus

Set up table:

dijktable

Vertex K has four neighbors: H, I, J and L Found at least one path to each of these vertices

dijkt1

We have found the shortest path from vertex K to each of the other vertices. Using the previous pointers, we can reconstruct the paths

dijkt2

This table defines a rooted parental tree

dijktree

What if at some point, all unvisited vertices have a distance \(\infty\)?

Implementation and Analysis

The initialization requires \(\Theta(\lvert V\rvert)\) memory and runtime Iterate \(\lvert V\rvert-1\) times, each time finding next closest vertex to the source

To do better: We only need the closest vertex

How about a priority queue?

The initialization still requires \(\Theta(\lvert V\rvert)\) memory and runtime - The priority queue will also requires \(O(\lvert V\rvert)\) memory - We must use an adjacency list, not an adjacency matrix

We iterate \(\lvert V\rvert\) times, each time finding the closest vertex to the source - Place the distances into a priority queue - The size of the priority queue is \(O(\lvert V\rvert)\) - Thus, the work required for this \(O(\lvert V\rvert \lg \lvert V\rvert)\)

Is this all the work that is necessary? - Each edge visited may result in a new edge being pushed to the very top of the heap - Thus, the work required for this is \(O(\lvert E\rvert \lg \lvert V\rvert)\) Total runtime: \(O(\lvert V\rvert \lg(\lvert V\rvert)+\lvert E\rvert \lg(\lvert V\rvert))=O(\lvert E\rvert \lg(\lvert V\rvert))\)

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 13-2 다익스트라 알고리즘과 복잡도 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: