indegree
와 outdegree
로 구분된다⇒
O(1)
즉, 상수의 시간 복잡도를 갖는다O(N)
의 시간 복잡도를 갖는다 → N개의 노드 모두 연결된 노드를 방문할 때는 O(N^2)
의 시간복잡도for(int i = 0; i < edge; i++)
{
int a, b;
a = sc.nextInt();
b = sc.nextInt();
adj[a][b] = 1;
//adj[b][a] = 1;
//무방향 그래프일 때는
//노드 a에서 노드 b로 가는 간선이 존재한다면
//노드 b에서 노드 a로 가는 간선도 존재한다고 생각할 수 있음
}
cf) 리스트에서 adj[1]에 있는 3개의 노드의 순서는 의미 X 4, 3, 2 순서여도 무관하다. 하지만 보기 좋게 오름차순으로 정렬한 것
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<=n;i++) {
adj.add(new ArrayList<Integer>());
}
for(int i=0;i<m;i++) {
int a = sc.nextInt();
int b = sc.nextInt();
adj.get(v1).add(v2);
adj.get(v2).add(v1); //무방향 그래프의 경우
}
Stack
이용Queue
이용void dfs(ArrayList<ArrayList<Integer>> adj, boolean[] check, int now) {
if(check[now] == true) return; // 재귀호출 종료 부, 방문한 적이 있으면 메소드 종료
check[now] = true; // 방문한 적 없는 정점이라면 방문 처리
System.out.println(now+"번 노드 방문"); // 방문한 정점 출력
// 바깥쪽 ArrayList의 인덱스인 정점과 연결된 정점을 탐색
for(int i = 0; i < adj.get(now).size(); i++) {
int next = adj.get(now).get(i);
if(!check[next]) // 연결된 정점이 방문한 적 없다면
dfs(adj, check, next); // 재귀호출로 깊이 우선 탐색 실행
}
}
Queue
void bfs(ArrayList<ArrayList<Integer>> adj, boolean[] check, int start) {
Queue<Integer> q = new LinkedList<>(); // BFS를 위한 큐 객체
q.add(start); // 시작 정점을 큐에 삽입
check[start] = true; // 방문처리를 한다.
while(!q.isEmpty()) { // 큐가 빌 때 까지 반복
int now = q.poll(); // 큐에 삽입되어 있는 정점을 하나 가져온다.
System.out.println(now+"번 노드 방문"); // 해당 정점을 출력
// 바깥쪽 ArrayList의 인덱스인 정점과 연결된 정점을 탐색해 모두 큐에 삽입
for(int i = 0; i < adj.get(now).size(); i++) {
int next = adj.get(now).get(i);
if(!check[next]) { // 방문하지 않았던 정점이라면
q.add(next);
check[next] = true; // 해당 정점을 방문 처리한다.
}
}
}
}
import java.util.*;
class Main {
static void dfs(ArrayList<ArrayList<Integer>> adj, boolean[] check, int now) {
if(check[now] == true) return;
check[now] = true;
System.out.print(now+" ");
for(int i = 0;i<adj.get(now).size();i++) {
int next = adj.get(now).get(i);
if(!check[next])
dfs(adj, check, next);
}
}
static void bfs(ArrayList<ArrayList<Integer>> adj, boolean[] check, int start) {
Queue<Integer> q = new LinkedList<>();
q.add(start);
check[start] = true;
while(!q.isEmpty()) {
int now = q.poll();
System.out.print(now+" ");
for(int i = 0;i<adj.get(now).size();i++) {
int next = adj.get(now).get(i);
if(!check[next]) {
q.add(next);
check[next] = true;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int start = sc.nextInt();
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
boolean check[] = new boolean[n+1];
for(int i=0;i<=n;i++) {
adj.add(new ArrayList<Integer>());
}
for(int i=0;i<m;i++) {
int v1 = sc.nextInt();
int v2 = sc.nextInt();
adj.get(v1).add(v2);
adj.get(v2).add(v1);
}
for(int i=1;i<=n;i++) {
Collections.sort(adj.get(i));
}
dfs(adj,check,start);
Arrays.fill(check, false);
System.out.println();
bfs(adj,check,start);
}
}
static int[] dx = {0, -1, 0, 1};//우 상 하 좌
static int[] dy = {1, 0, -1, 0};
static void bfs(int x, int y) {
Queue<int[]> q = new LinkedList<int[]>();
q.add(new int[]{x, y});
while (!q.isEmpty()) {
x = q.peek()[0];
y = q.peek()[1];
visit[x][y] = true;
q.poll();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < M && 0 <= ny && ny < N) {
if (!visit[nx][ny] && map[nx][ny] == 1) {
q.add(new int[]{nx, ny});
visit[nx][ny] = true;
}
}
}
}
}