Skip to content

Latest commit

ย 

History

History
137 lines (105 loc) ยท 4.25 KB

0722-dfs.md

File metadata and controls

137 lines (105 loc) ยท 4.25 KB

2020.07.22 Wed

๐Ÿ“š๊ณต๋ถ€ํ•œ ๊ฑฐ List๐Ÿ“š

  • ์ข…๋งŒ๋ถ - 28. ๊ทธ๋ž˜ํ”„์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

[์ข…๋งŒ๋ถ] ๊ทธ๋ž˜ํ”„์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

DFS_Depth First Search

โŒจ๏ธ๊ทธ๋ž˜ํ”„์˜ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

vector<vector<int>> adj;
vector<bool> visited;

void dfs(int here) {
  visited[here] = true;
  for(int i=0; i<adj[here].size(); ++i) {
    int there = adj[here][i];
    if(!visited[there]) dfs(there);
  }
}

void dfsAll() {
  visited = vector<int>(adj.size(), false);
  for(int i=0; i<adj.size(); ++i) {
    if(!visited[i]) dfs(i);
  }
}

Time/Space Complexity

์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ : O(|V|+|E|)

์ธ์ ‘ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ : O(|V|^2)

ํฌ์†Œํ–‰๋ ฌ(spare matrix)์ธ ๊ฒฝ์šฐ์—๋Š” ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ, ๋ฐ€์ง‘ํ–‰๋ ฌ(dense matrix)์ธ ๊ฒฝ์šฐ์—๋Š” ์ธ์ ‘ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ณต๊ฐ„์˜ ๋‚ญ๋น„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์œ„์ƒ์ •๋ ฌ

์˜์กด์„ฑ ๊ทธ๋ž˜ํ”„(Dependency Graph) : ๊ฐ ์ž‘์—…์„ ์ •์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์ž‘์—… ๊ฐ„์˜ ์˜์กด ๊ด€๊ณ„๋ฅผ ๊ฐ„์„ ์œผ๋กœ ํ‘œํ˜„ํ•œ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„

DAG : ์‚ฌ์ดํด ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„.

DAG์˜ ์ •์ ์„ ๋ฐฐ์—ดํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์œ„์ƒ์ •๋ ฌ(Topological Sort)๋ผ๊ณ  ํ•œ๋‹ค.

dfs()๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๋งˆ๋‹ค ์ •์ ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ด๋‘๊ณ , dfsAll()์ด ์ข…๋ฃŒํ•œ ๋’ค ๊ธฐ๋ก๋œ ์ •์ ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์œผ๋ฉด ์œ„์ƒ์ •๋ ฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋œ๋‹ค.

๐Ÿ“์˜ˆ์ œ::๊ณ ๋Œ€์–ด ์‚ฌ์ „(ID: DICTIONARY)

๋ฌธ์ œ : ๋‹จ์–ด๋“ค์˜ ๋ชฉ๋ก์ด ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์งˆ ๋•Œ ์ด ์–ธ์–ด์—์„œ ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ๋ฅผ ๊ณ„์‚ฐ.

โšก๏ธkeypointโšก๏ธ

  1. ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ •์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ , ์ˆœ์„œ๋Š” ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•œ๋‹ค.
  2. ์‚ฌ์ดํด์ด ์žˆ๋‹ค๋ฉด ์œ„์ƒ์ •๋ ฌ์ด ์•„๋‹ˆ๋‹ค. ์‚ฌ์ดํด์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ๊ฐ„์„ ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉด๋œ๋‹ค.

๐Ÿ”‘**ํ•ด๋‹ต(์ฝ”๋“œ)**๐Ÿ”‘

/*
 * ๊ณ ๋Œ€์–ด ์‚ฌ์ „ ๋ฌธ์ œ์˜ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
 */
vector<vector<int>> adj;
void makeGraph(const vector<string>& words) {
  adj = vector<vector<int>>(26,vector<int>(26,0));
  for(int i=1; i<words.size(); ++i) {
    int j=i-1, len = min(words[i].size(), words[j].size());
    for(int k=0; k<len; ++k) {
      if(words[i][k] != words[j][k]){
        int a = words[i][k] - 'a';
        int b = words[j][k] - 'a';
        adj[a][b] = 1;
        break;
      }
    }
  }
}
//๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์„ ์ด์šฉํ•œ ์œ„์ƒ์ •๋ ฌ
vector<int> visited, order;
void dfs(int here) {
  visited[here] = 1;
  for(int there = 0; there<adj.size(); ++there) { //์ „์ฒด ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋งŒํผ
    //๋งŒ์•ฝ here->there ๊ฐ„์„ ์ด ์žˆ๊ณ , ์•„์ง there์„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, there ๋ฐฉ๋ฌธ
    if(adj[here][there] && !visited[there]) dfs(there);
  }
  order.push_back(here); //order์— here ์ถ”๊ฐ€
}

vector<int> topologicalSort() {
  visited = vector<int>(adj.size(), false); //์ดˆ๊ธฐํ™”
  order.clear(); //์ˆœ์„œ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
  for(int i=0; i<adj.size(); ++i) { //์ „์ฒด๋…ธ๋“œ์˜ ์ˆ˜๋งŒํผ
    if(!visited[i]) dfs(i); //i-๋…ธ๋“œ ๋ฐฉ๋ฌธ์•ˆํ–ˆ๋‹ค๋ฉด, ๋ฐฉ๋ฌธํ•˜๊ธฐ
  }
  //#include <algorithm>
  reverse(order.begin(), order.end()); //order ์ˆœ์„œ ๊ฑฐ๊พธ๋กœ ์ •๋ ฌ
  //์—ญ๋ฐฉํ–ฅ ๊ฐ„์„ ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์žˆ๋‹ค๋ฉด ๋นˆ ๋ฒกํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
  for(int i=0; i<adj.size(); ++i) { 
    for(int j=i+1; j<adj.size(); ++j) { 
      if(adj[order[j]][order[i]]) return vector<int>();
    }
  } 
  return order;
}

์˜ค์ผ๋Ÿฌ ์„œํ‚ท(Euler Circuit)

๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๊ฐ„์„ ์„ ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ์”ฉ ์ง€๋‚˜์„œ ์‹œ์ž‘์ ์œผ๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

  • ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ๋“ค์ด ์ง์ˆ˜์ ์ด์–ด์•ผ๋งŒ ์˜ค์ผ๋Ÿฌ ์„œํ‚ท์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • findRandomCircuit(u) : ์ •์  u์—์„œ ์‹œ์ž‘ํ•ด ์•„์ง ๋”ฐ๋ผ๊ฐ€์ง€ ์•Š์€ ๊ฐ„์„  ์ค‘ ํ•˜๋‚˜๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉฐ ์ž„์˜์˜ ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜/๋”์ด์ƒ ๋‚จ์€ ๊ฐ„์„ ์ด ์—†์„๋•Œ๊นŒ์ง€.

โŒจ๏ธ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์„ ์ด์šฉํ•œ ์˜ค์ผ๋Ÿฌ ์„œํ‚ท ์ฐพ๊ธฐ ๐Ÿ’ฆ

vector<vector<int>> adj;
void getEulerCircuit(int here, vector<int>& circuit) {
  for(int there=0; there<adj[here].size(); ++there) {
    while(adj[here][there]>0){
      adj[here][there]--;
      adj[there][here]--;
      getEulerCircuit(there, circuit);
    }
  }
  circuit.push_back(here);
}