Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 객체 지향 설계와 스프링
- Kotlin in action 3장
- Kotlin in action 10장
- 20055 컨베이어 벨트 위의 로봇
- 컨베이어 벨트 위의 로봇 Python
- 싱글톤 컨테이너
- 고급매핑
- KotlinInAction
- 스프링 핵심 원리 - 기본편
- 백준 13460 Python
- 스프링 컨테이너와 스프링 빈
- kotlin in action 정리
- 백준 20055 컨베이어 벨트 위의 로봇
- Kotlin in action 6장
- 코틀린
- 자바 ORM 표준 JPA 프로그래밍 7장
- 스프링 핵심 원리 이해
- Kotlin in action 5장
- 코틀린인액션
- 기능개발 python
- Kotlin
- 13460 구슬탈출 2
- 7장 고급매핑
- 백준
- spring
- 20055
- Python
- Kotlin In Action
- 코틸린인액션
- 스프링 핵심 원리
Archives
- Today
- Total
기록하는 습관
[알고리즘] 그래프(Graph) 구현 본문
그래프의 구현
- 인접 행렬을 이용한 구현 : O(V * V)
- 인접 리스트를 이용한 구현 : O(V+E)
* 적은 간선을 가질 경우(Sparse Graph) 인접 리스트가 더 효율적이다.
#include <iostream>
#include <vector>
#include <tuple>
#define SIZE 500
using namespace std;
int main() {
int vertexN, edgeN; // 정점의 갯수, 간선의 갯수
cin >> vertexN >> edgeN;
vector<tuple<int, int, int>> edge(edgeN);
// 간선의 정보를 저장
for (int i = 0; i < edgeN; i++) {
int a, b, w; // a -> b 가중치 : w
scanf("%d %d %d", &a, &b, &w);
edge.push_back(make_tuple(a, b, w));
}
// 행렬
int graphMatrix[SIZE + 1][SIZE + 1];
for (auto i : edge) {
int v1 = get<0>(i); // a
int v2 = get<1>(i); // b
int w = get<2>(i); // w
// 양방향 기록. 단방향의 경우 한 줄만 작성
graphMatrix[v1][v2] = w;
graphMatrix[v2][v1] = w;
}
// 리스트
vector<pair<int, int>> graphList[SIZE + 1];
for (auto i : edge) {
int v1 = get<0>(i); // a
int v2 = get<1>(i); // b
int w = get<2>(i); // w
// 양방향 기록. 단방향의 경우 한 줄만 작성
graphList[v1].push_back(make_pair(v2, w));
graphList[v2].push_back(make_pair(v1, w));
}
}
참고 블로그 : https://coderkoo.tistory.com/11
'알고리즘 > [개념] 알고리즘' 카테고리의 다른 글
[알고리즘] DFS, BFS Python 핵심 코드 정리 (0) | 2021.06.06 |
---|---|
[알고리즘] 탐색 - DFS vs BFS (0) | 2020.01.09 |
[알고리즘] 탐색 - BFS (0) | 2020.01.09 |
[알고리즘] 탐색 - DFS(C++) (0) | 2020.01.08 |
[알고리즘] Greedy Algorithm (0) | 2020.01.07 |
Comments