2020-1ν•™κΈ°, λŒ€ν•™μ—μ„œ β€˜μ•Œκ³ λ¦¬μ¦˜β€™ μˆ˜μ—…μ„ λ“£κ³  κ³΅λΆ€ν•œ λ°”λ₯Ό μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€. 지적은 μ–Έμ œλ‚˜ ν™˜μ˜μž…λ‹ˆλ‹€ :)

3 minute read

2020-1ν•™κΈ°, λŒ€ν•™μ—μ„œ β€˜μ•Œκ³ λ¦¬μ¦˜β€™ μˆ˜μ—…μ„ λ“£κ³  κ³΅λΆ€ν•œ λ°”λ₯Ό μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€. 지적은 μ–Έμ œλ‚˜ ν™˜μ˜μž…λ‹ˆλ‹€ :)

<DFS>, <BFS>에 λŒ€ν•œ 기초 λ°±μ€€ 문제인 1260번 - DFS와 BFS κΈ°μ€€μœΌλ‘œ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ³΄μ•˜λ‹€.

Set-up

기본적으둜 본인은 <edge>λ₯Ό μ €μž₯ν•  λ•Œ, <인접 리슀트 Adjacency List> ν˜•νƒœλ₯Ό μ‚¬μš©ν•œλ‹€. 그리고 λ°©λ¬Έ μ—¬λΆ€λŠ” bool νƒ€μž…μ˜ λ°°μ—΄λ‘œ κΈ°λ‘ν•œλ‹€.

vector<int> edges[MAX];
bool isVisit[MAX];

...

for (int i = 0; i < M; i++) {
  int s, e;
  scanf("%d %d", &s, &e);
  edges[s].push_back(e);
  edges[e].push_back(s);
}

DFS (recurrsion)

void dfs(int v) {
  printf("%d ", v);
  isVisit[v] = true;

  for (int i = 0; i < edges[v].size(); i++) {
    int adj = edges[v][i];
    if (!isVisit[adj]) {
      dfs(adj);
    }
  }
}

DFS (stack)

void dfs_stack(int s) {
  stack<int> st;
  st.push(s);

  while (!st.empty()) {
    int top = st.top();
    st.pop();

    if(!isVisit[top]){
      isVisit[top] = true;
      printf("%d ", top);

      for (int i = 0; i < edges[top].size(); i++) {
        int adj = edges[top][i];
        if (!isVisit[adj]) {
          st.push(adj);
        }
      }
    }
  }
}

개인적으둜 λΉ„μΆ”ν•˜λŠ” κ΅¬ν˜„μ΄λ‹€.

BFS (queue)

void bfs(int s) {
  queue<int> q;
  isVisit[s] = true;
  q.push(s);

  while (!q.empty()) {
    int front = q.front();
    printf("%d ", front, q.size());
    q.pop();

    for (int i = 0; i < edges[front].size(); i++) {
      int adj = edges[front][i];
      if (!isVisit[adj]) {
        q.push(adj);
        isVisit[adj] = true;
      }
    }
  }
}

DFS-BFS ν˜Έν™˜ μ½”λ“œ

μˆ˜λ…„ κ°„μ˜ PS κ²½ν—˜μ— λ”°λ₯΄λ©΄, <DFS>의 κ΅¬ν˜„ μ½”λ“œμ™€ <BFS>의 κ΅¬ν˜„ μ½”λ“œμ˜ ν˜•νƒœλ₯Ό 거의 μΌμΉ˜μ‹œν‚¬ 수 μžˆλŠ” κ΅¬ν˜„ μŠ€νƒ€μΌμ΄ μžˆλ‹€. 이 μŠ€νƒ€μΌμ€ κ΅¬ν˜„μ—μ„œ stack을 queue둜 λ°”κΎΈλ©΄ <DFS>μ—μ„œ <BFS>κ°€ 되고, κ·Έ λ°˜λŒ€λ‘œ λ³€ν™˜ν•˜λ©΄, <BFS>μ—μ„œ <DFS>κ°€ λœλ‹€!!

1. DFS (stack)

void dfs_stack(int s) {
  stack<int> st; // stack μ‚¬μš©
  st.push(s);

  while (!st.empty()) {
    int top = st.top();
    st.pop();

    if(!isVisit[top]){
      isVisit[top] = true;
      printf("%d ", top);

      for (int i = 0; i < edges[top].size(); i++) {
        int adj = edges[top][i];
        if (!isVisit[adj]) {
          st.push(adj);
        }
      }
    }
  }
}

2. BFS (queue)

void bfs_queue(int s) {
  queue<int> q; // queueλ₯Ό μ‚¬μš”!
  q.push(s);

  while (!q.empty()) {
    int front = q.front();
    q.pop();

    if(!isVisit[front]){
      isVisit[front] = true;
      printf("%d ", front, q.size());

      for (int i = 0; i < edges[front].size(); i++) {
        int adj = edges[front][i];
        if (!isVisit[adj]) {
          q.push(adj);
        }
      }
    }
  }
}

μ½”λ“œλ₯Ό 잘 μ‚΄νŽ΄λ³΄λ©΄, <Container>둜 stack을 μ„ νƒν•˜λŠ”μ§€, queueλ₯Ό μ„ νƒν•˜λŠ”μ§€μ— 따라 μ•Œκ³ λ¦¬μ¦˜μ΄ λ°”λ€Œμ—ˆλ‹€!!!

개인적으둜 자주 μ“°λŠ” μŠ€νƒ€μΌμ€ μ•„λ‹Œλ°, <DFS>, <BFS> μ½”λ“œλ₯Ό μ—¬λŸ¬ 번 κ΅¬ν˜„ν•΄λ³΄λ‹ˆ 이런 νŒ¨ν„΄λ„ 보이게 λ˜μ—ˆλ‹€ γ…Žγ…Ž 😎

ν•˜μ§€λ§Œ μ—¬μ „νžˆ <DFS> κ΅¬ν˜„μ€ <μž¬κ·€ recursion>으둜 κ΅¬ν˜„ν•˜λŠ” κ±Έ μ„ ν˜Έν•˜λŠ” 것 κ°™λ‹€ πŸ˜†

Categories:

Updated: