SlideShare a Scribd company logo
By	POSTECH	Computer	Algorithm	Team
그래프2
이건규
Graph
POSTECH Computer Algorithm Team
Tree
우선순위 큐
상호 배타적 집합
크루스칼
프림
이번에 배울 알고리즘
왼쪽은 저번주
Graph
bfs
dfs
다익스트라
플로이드-워셜
오일러 트레일
해밀턴 경로
POSTECH Computer Algorithm Team
- 그래프 G(V,E)와 임의의 node	a가 주어질 때,	a로 부터 차례대로 그래프의 node를 탐색하는 알고리즘
- Bfs :	너비 우선 탐색,	탐색하는 node와 a의 거리가 순차적으로 증가
- Dfs :	깊이 우선 탐색,	탐색하는 node와 a의 거리가 증감 반복
BFS DFS란?
탐색 알고리즘
POSTECH Computer Algorithm Team
BFS 모형
POSTECH Computer Algorithm Team
- Queue를 사용
- 시작 점을 queue에 push
- Queue가 빌 때까지 queue.front의 인접한 node중 방문하지 않는 node를 방문 ->	queue에 push	+	방문 기록
- Queue.front 탐색
- Queue.pop()
- 위 반복
- 시간 복잡도는?
- O(V+E)
BFS 구현
어떻게 할까여
POSTECH Computer Algorithm Team
DFS 모형
POSTECH Computer Algorithm Team
- 재귀함수 혹은 stack으로 구현
- 재귀함수 구현은 쉬우나 시간,	메모리 면에서 손해다.	Stack은 어려우나 성능이 좋다
- 재귀함수 :	인자로 현재 node와 인접 행렬,	방문 기록을 지님
- 현재 node를 방문,	탐색1
- 현재 node에서 갈 수 있고,	방문하지 않은 node를 인자로 함수 호출
- 현재 node	탐색2
- 시간 복잡도?
- O(V+E)
DFS 구현
어케하누
POSTECH Computer Algorithm Team
- 그래프 G(V,E,W)의 두 node	a,b 사이의 거리의 최소를 구하는 알고리즘
- 다익스트라(dijkstra) :	한 node와의 거리만을 구함
- 플로이드-워셜(Floyd-Warshall)	:	그래프의 모든 가능한 쌍에 대하여 계산
- 단 모든 w >=	0
- Why?
최단 경로 알고리즘
거리 탐색 알고리즘
POSTECH Computer Algorithm Team
다익스트라 모형
Dijkstra
POSTECH Computer Algorithm Team
- 우선순위 큐를 사용
- 가장 거리가 짧은 node를 반환
- (시작점,0)을 push
- Pq가 빌 때까지 pq.front 방문 ->	거리 저장 (단 방문한 적 없는 경우만)
- Pq.front와 연결된 곳 중 방문하지 않은 곳을 push
- 반복
- 시간 복잡도?
- O(ElogE)	=	O(ElogV)	~=	O(V2logV)
다익스트라 구현
Dijktra
POSTECH Computer Algorithm Team
플로이드 워셜 모형
그딴 거 없다
POSTECH Computer Algorithm Team
- DP의 연장선
- 각 k	node를 업데이트
- 모든 i,j는 k를 포함한 거리를 가진다.	->	모든 node를 포함한 거리
- 시간 복잡도 O(N3)	=	O(V3)
플로이드 워셜 구현
Floyd-Warshall
POSTECH Computer Algorithm Team
- 그래프 G(V,E)의 모든 edge를 지나는 circuit
- 한붓그리기 문제로 유명하다
- Odd	degree인 node가 0개 혹은 2개인 경우 해가 존재한다
- 아무렇게나 circuit 제작 – 가능함이 증명
- 더 이상 X	->	시작점으로 돌아오거나 odd	degree에 도착
- 경로를 다시 살펴보며 추가 가능한 경우 위처럼 추가
- 시간복잡도 O(VE)
오일러 경로
Eulerian trail
POSTECH Computer Algorithm Team
- 모든 node를 한번씩 지나는 path
- How?
- NP	완비 문제라 다항 시간 안에 못 품
해밀턴 경로
Hamilton path
POSTECH Computer Algorithm Team
https://www.acmicpc.net/problem/1260 - bfs,	dfs 기초
https://www.acmicpc.net/problem/7576 - bfs
https://www.acmicpc.net/problem/7569 - bfs 심화
https://www.acmicpc.net/problem/14868 - bfs +	union	find
https://www.acmicpc.net/problem/1753 - 다익스트라
https://www.acmicpc.net/problem/2606 - 플로이드 워셜
예시 문제
problem

More Related Content

What's hot

2020 여름방학 정기스터디 6주차
2020 여름방학 정기스터디 6주차2020 여름방학 정기스터디 6주차
2020 여름방학 정기스터디 6주차
Moonki Choi
 
Number theory
Number theoryNumber theory
Number theory
DavidPark267
 
실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬
현찬 양
 
1. alps c&c++
1. alps c&c++1. alps c&c++
1. alps c&c++
Hongjun Jang
 
Lec 00, 01
Lec 00, 01Lec 00, 01
Lec 00, 01
Jinhwan Suk
 
그래프의 최단 경로 찾기
그래프의 최단 경로 찾기그래프의 최단 경로 찾기
그래프의 최단 경로 찾기
Jung-Ho Kim
 
Doing mathwithpython.ch02
Doing mathwithpython.ch02Doing mathwithpython.ch02
Doing mathwithpython.ch02
Seok-joon Yun
 
PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation
민웅 이
 
A tour of go
A tour of goA tour of go
A tour of go
Mungyu Choi
 
Doing math with python.ch05
Doing math with python.ch05Doing math with python.ch05
Doing math with python.ch05
Seok-joon Yun
 
DNN-Based Prediction Model for Spatial-Temporal Data
DNN-Based Prediction Model for Spatial-Temporal DataDNN-Based Prediction Model for Spatial-Temporal Data
DNN-Based Prediction Model for Spatial-Temporal Data
HWANGTAEYONG
 
게임에서 사용할 수 있는 포물선 운동
게임에서 사용할 수 있는 포물선 운동게임에서 사용할 수 있는 포물선 운동
게임에서 사용할 수 있는 포물선 운동세민 이
 
On lisp ch18
On lisp ch18On lisp ch18
On lisp ch18
EunPyoung Kim
 

What's hot (13)

2020 여름방학 정기스터디 6주차
2020 여름방학 정기스터디 6주차2020 여름방학 정기스터디 6주차
2020 여름방학 정기스터디 6주차
 
Number theory
Number theoryNumber theory
Number theory
 
실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬실전프로젝트 정서경 양현찬
실전프로젝트 정서경 양현찬
 
1. alps c&c++
1. alps c&c++1. alps c&c++
1. alps c&c++
 
Lec 00, 01
Lec 00, 01Lec 00, 01
Lec 00, 01
 
그래프의 최단 경로 찾기
그래프의 최단 경로 찾기그래프의 최단 경로 찾기
그래프의 최단 경로 찾기
 
Doing mathwithpython.ch02
Doing mathwithpython.ch02Doing mathwithpython.ch02
Doing mathwithpython.ch02
 
PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation
 
A tour of go
A tour of goA tour of go
A tour of go
 
Doing math with python.ch05
Doing math with python.ch05Doing math with python.ch05
Doing math with python.ch05
 
DNN-Based Prediction Model for Spatial-Temporal Data
DNN-Based Prediction Model for Spatial-Temporal DataDNN-Based Prediction Model for Spatial-Temporal Data
DNN-Based Prediction Model for Spatial-Temporal Data
 
게임에서 사용할 수 있는 포물선 운동
게임에서 사용할 수 있는 포물선 운동게임에서 사용할 수 있는 포물선 운동
게임에서 사용할 수 있는 포물선 운동
 
On lisp ch18
On lisp ch18On lisp ch18
On lisp ch18
 

Similar to Graph2

04. network flow 1
04. network flow   104. network flow   1
04. network flow 1
승혁 조
 
Network flow
Network flowNetwork flow
Network flow
상조 김
 
Tree algorithm
Tree algorithmTree algorithm
Tree algorithm
승혁 조
 
DP 알고리즘에 대해 알아보자.pdf
DP 알고리즘에 대해 알아보자.pdfDP 알고리즘에 대해 알아보자.pdf
DP 알고리즘에 대해 알아보자.pdf
Ho Jeong Im
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
Bill Kim
 
헤테로지니어스 컴퓨팅 : CPU 에서 GPU 로 옮겨가기
헤테로지니어스 컴퓨팅 :  CPU 에서 GPU 로 옮겨가기헤테로지니어스 컴퓨팅 :  CPU 에서 GPU 로 옮겨가기
헤테로지니어스 컴퓨팅 : CPU 에서 GPU 로 옮겨가기zupet
 

Similar to Graph2 (6)

04. network flow 1
04. network flow   104. network flow   1
04. network flow 1
 
Network flow
Network flowNetwork flow
Network flow
 
Tree algorithm
Tree algorithmTree algorithm
Tree algorithm
 
DP 알고리즘에 대해 알아보자.pdf
DP 알고리즘에 대해 알아보자.pdfDP 알고리즘에 대해 알아보자.pdf
DP 알고리즘에 대해 알아보자.pdf
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
 
헤테로지니어스 컴퓨팅 : CPU 에서 GPU 로 옮겨가기
헤테로지니어스 컴퓨팅 :  CPU 에서 GPU 로 옮겨가기헤테로지니어스 컴퓨팅 :  CPU 에서 GPU 로 옮겨가기
헤테로지니어스 컴퓨팅 : CPU 에서 GPU 로 옮겨가기
 

Graph2

  • 2. POSTECH Computer Algorithm Team Tree 우선순위 큐 상호 배타적 집합 크루스칼 프림 이번에 배울 알고리즘 왼쪽은 저번주 Graph bfs dfs 다익스트라 플로이드-워셜 오일러 트레일 해밀턴 경로
  • 3. POSTECH Computer Algorithm Team - 그래프 G(V,E)와 임의의 node a가 주어질 때, a로 부터 차례대로 그래프의 node를 탐색하는 알고리즘 - Bfs : 너비 우선 탐색, 탐색하는 node와 a의 거리가 순차적으로 증가 - Dfs : 깊이 우선 탐색, 탐색하는 node와 a의 거리가 증감 반복 BFS DFS란? 탐색 알고리즘
  • 4. POSTECH Computer Algorithm Team BFS 모형
  • 5. POSTECH Computer Algorithm Team - Queue를 사용 - 시작 점을 queue에 push - Queue가 빌 때까지 queue.front의 인접한 node중 방문하지 않는 node를 방문 -> queue에 push + 방문 기록 - Queue.front 탐색 - Queue.pop() - 위 반복 - 시간 복잡도는? - O(V+E) BFS 구현 어떻게 할까여
  • 6. POSTECH Computer Algorithm Team DFS 모형
  • 7. POSTECH Computer Algorithm Team - 재귀함수 혹은 stack으로 구현 - 재귀함수 구현은 쉬우나 시간, 메모리 면에서 손해다. Stack은 어려우나 성능이 좋다 - 재귀함수 : 인자로 현재 node와 인접 행렬, 방문 기록을 지님 - 현재 node를 방문, 탐색1 - 현재 node에서 갈 수 있고, 방문하지 않은 node를 인자로 함수 호출 - 현재 node 탐색2 - 시간 복잡도? - O(V+E) DFS 구현 어케하누
  • 8. POSTECH Computer Algorithm Team - 그래프 G(V,E,W)의 두 node a,b 사이의 거리의 최소를 구하는 알고리즘 - 다익스트라(dijkstra) : 한 node와의 거리만을 구함 - 플로이드-워셜(Floyd-Warshall) : 그래프의 모든 가능한 쌍에 대하여 계산 - 단 모든 w >= 0 - Why? 최단 경로 알고리즘 거리 탐색 알고리즘
  • 9. POSTECH Computer Algorithm Team 다익스트라 모형 Dijkstra
  • 10. POSTECH Computer Algorithm Team - 우선순위 큐를 사용 - 가장 거리가 짧은 node를 반환 - (시작점,0)을 push - Pq가 빌 때까지 pq.front 방문 -> 거리 저장 (단 방문한 적 없는 경우만) - Pq.front와 연결된 곳 중 방문하지 않은 곳을 push - 반복 - 시간 복잡도? - O(ElogE) = O(ElogV) ~= O(V2logV) 다익스트라 구현 Dijktra
  • 11. POSTECH Computer Algorithm Team 플로이드 워셜 모형 그딴 거 없다
  • 12. POSTECH Computer Algorithm Team - DP의 연장선 - 각 k node를 업데이트 - 모든 i,j는 k를 포함한 거리를 가진다. -> 모든 node를 포함한 거리 - 시간 복잡도 O(N3) = O(V3) 플로이드 워셜 구현 Floyd-Warshall
  • 13. POSTECH Computer Algorithm Team - 그래프 G(V,E)의 모든 edge를 지나는 circuit - 한붓그리기 문제로 유명하다 - Odd degree인 node가 0개 혹은 2개인 경우 해가 존재한다 - 아무렇게나 circuit 제작 – 가능함이 증명 - 더 이상 X -> 시작점으로 돌아오거나 odd degree에 도착 - 경로를 다시 살펴보며 추가 가능한 경우 위처럼 추가 - 시간복잡도 O(VE) 오일러 경로 Eulerian trail
  • 14. POSTECH Computer Algorithm Team - 모든 node를 한번씩 지나는 path - How? - NP 완비 문제라 다항 시간 안에 못 품 해밀턴 경로 Hamilton path
  • 15. POSTECH Computer Algorithm Team https://www.acmicpc.net/problem/1260 - bfs, dfs 기초 https://www.acmicpc.net/problem/7576 - bfs https://www.acmicpc.net/problem/7569 - bfs 심화 https://www.acmicpc.net/problem/14868 - bfs + union find https://www.acmicpc.net/problem/1753 - 다익스트라 https://www.acmicpc.net/problem/2606 - 플로이드 워셜 예시 문제 problem