[Algorithm] Dijkstra

2021. 2. 14. 00:22·Algorithm/Algorithm (이론)

 

When to use?

  • 임의의 한 정점에서 모든 정점으로의 최단 경로 및 비용을 구할 때.
  • 특정 출발 지점에서 도착지점까지의 최단 경로 및 비용을 구할 때

# shortest path # greedy

 

Algorithm

1. Create shortestPath Set, distance array table || (visited array)

2. initialize all vertices with make start node weight '0', reamin node weight 'INF'

3. set || priorityQueue != empty

   (a) Pick minimum vertex 

   (b) check all adjacent edge weight

     compare distance [adjacentVertex] vs distance[minimumVertex] + adjacentVertex weight

      => '<' update , >= nothing to do

   (c) set.insert || priorityQueue.insert()

 

⚠️Regardless of kind of graph (either undirected or directed). you need checking exsistence of target vertex in set using find function 
∵ There is possibility abscence of vertex (already poped from set or priority queue) 
// if already meet visited node-> ignore it since this is undirected graph
if (distanceSet.find(Node{adjacentVertex[DESTINATION_NODE], nodes[adjacentVertex[DESTINATION_NODE]].weight}) == distanceSet.end()) continue;

There is possibility that multiple access to node '7'  (3 times above graph.) ->in set or priority queue or visited boolean array

 

🖊️ use STL Set || Priority Queue for choosing minimum vertex.

 

Time Complexity

adjacent matrix -> O (V^2)
adjacent list + Priority Queue || Set -> O(E Log V)

 

 

 

Implement Dijkstra using Set

header

cpp

main


Reference

 

Dijkstra’s shortest path algorithm using set in STL - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

 

Dijkstra's Shortest Path Algorithm using priority_queue of STL - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

저작자표시 (새창열림)

'Algorithm > Algorithm (이론)' 카테고리의 다른 글

[Algorithm] Binary search  (0) 2022.09.02
[Data Structure] Binary Search Tree  (0) 2021.03.01
[Algorithm] Heap Sort  (0) 2021.02.11
[Data Structure] Priority Queue  (0) 2021.02.10
[Algorithm] Prim (feat. compare to Kruskal)  (0) 2021.02.10
'Algorithm/Algorithm (이론)' 카테고리의 다른 글
  • [Algorithm] Binary search
  • [Data Structure] Binary Search Tree
  • [Algorithm] Heap Sort
  • [Data Structure] Priority Queue
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • 전체
    오늘
    어제
    • 분류 전체보기 (432)
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (문제풀이) (56)
        • Algorithm (이론) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • 기타 (70)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13)
        • git (19)
        • 잡동사니 (27)
      • 재테크 (11)
        • 세무 (4)
        • 투자 (3)
        • 보험 (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • 시스템 보안 (5)
      • 네트워크 (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • 저작권과 스마트폰의 이해 (0)
      • 생각 뭉치 (6)
      • 궁금증 (2)
      • Private (4)
        • 이직 경험 (0)
        • 꿈을 찾아서 (1)
      • Android (21)
        • OS (4)
  • 블로그 메뉴

    • 홈
    • WEB
    • 알고리즘
    • DataBase
    • Linux
    • Mobile
    • C
    • 방명록
  • 링크

    • github
  • 공지사항

  • 인기 글

  • 태그

    프로그래머스
    algorithm
    linux
    javascript
    Kotlin
    database
    kafka
    Git
    Spring
    docker
    PostgreSQL
    Programmers
    Bitcoin
    ubuntu
    알고리즘
    java
    C++
    JPA
    android
    백준
  • hELLO· Designed By정상우.v4.10.3
M_Falcon
[Algorithm] Dijkstra
상단으로

티스토리툴바