일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Gap
- margin
- grid
- 확률
- align-items
- 소수
- 백준
- Flexbox
- spring
- SK바이오사이언스
- Prefix Sums
- 에라토스테네스의 체
- 강화학습
- 상태
- 수학
- 알고리즘
- CSS
- 통신사할인
- Design Pattern
- 포토샵
- Javascript
- Photoshop
- c
- series
- c++
- stl
- skt membership
- pandas
- Codility
- dataframe
Archives
- Today
- Total
sliver__
DFS & 다익스트라 본문
728x90
Depth First Search(DFS), 깊이 우선 탐색
- 그래프의 탐색에 사용되는 알고리즘
- 모든 경우의 수를 탐색할 때 사용
- Vertex의 순서를 고려할 때 사용
- 백트래킹에 사용
다익스트라 알고리즘
- 최단 거리 알고리즘
- 힙 사용(우선순위 큐)
- 가중치 있는 간선일 때 사용
priority_queue< pair<int, int> > pq;
pq.push(make_pair(0, 시작 정점)); // 시작 정점의 가중치는 0
dist[시작 정점] = 0;
while(!pq.empty())
{
int cur_vertex = pq.top().second;
int cur_dist = -pq.top().first; // 넣을 때 -를 붙여서 넣는다. 가장 작은 간선 가중치가 위로 올라오기 때문이다.
for(int i=0;i<v[cur_vertex].size();i++)
{
int next_dist = v[cur_vertex][i].dist; // 현재 정점 - 연결된 정점 간선의 가중치
int next_vertex = v[cur_vertex][i].vertex; // 연결된 정점의 정보
next_dist = next_dist + cur_dist; // 현재 위치 가중치 + 연결된 정점의 가중치 값 구하기
if(next_dist < dist[next_vertex]) // 현재 위치 가중치 + 연결된 정점의 가중치 값이 저장된 가중치 값보다 작으면 최단거리이므로 업데이트 !
{
dist[next_vertex] = next_dist;
pq.push(make_pair(-dist[next_vertex], next_vertex)); // 더 짧은 거리가 있을 수 있으므로 탐색 대상에 추가
}
}
}
728x90
'CS > 알고리즘' 카테고리의 다른 글
이분탐색 (binary search) / 파라메트릭 서치 (parametric search) (1) | 2023.10.02 |
---|---|
[프로그래머스] - 완주하지 못한 선수 (0) | 2021.12.07 |
백준 - 분해합(2231) (0) | 2021.11.19 |
백준 - 블랙잭(2798) (0) | 2021.11.19 |
백준 - 피보나치 수 5 (0) | 2021.11.18 |
Comments