seoyeong200 / LeetCode

0 stars 0 forks source link

graph algorithms #6

Open seoyeong200 opened 1 month ago

seoyeong200 commented 1 month ago

description

graph types - direction, weight, cycle, connection 유무에 따라 그래프 종륲 구분 graph traversals - BFS, DFS shortest path algorithms - Dijkstra, Bellman Ford, Floyd minimum spanning tree - prim, kruskal

seoyeong200 commented 1 month ago

graph traversals

seoyeong200 commented 1 month ago

shortest path

dijkstra & bellman-ford vs floyd

dijkstra vs bellman-ford

Dijkstra's algorithm

main points

Screenshot 2024-01-02 at 7 59 20 PM
seoyeong200 commented 1 month ago

Bellmand Ford

  1. distance 배열 inf 로 초기화, start node는 0으로 초기화
  2. repeat v-1 times - 각 (u, v) 간선에 대해서 (weight w) dist[u]+w<dist[v]dist[v]=dist[u]+w
  3. negative weight check - 2번 단계에서 distance 감소하면 negative-weight cycle 존재
seoyeong200 commented 1 month ago

Floyd Warshall Algorithm

그래서 기본적으로는 알고리즘이 아래와 같은 형태다.

# create graph array : N*N array, graph[i][j] = i에서 j로 가는 최단경로
for i in range(n): # proxy vertex
    for j in range(n): # start vertex
        for k in range(n): # end vertex
            graph[j][k] = min(graph[j][k], graph[j][i] + graph[i][k])