SlideShare a Scribd company logo
1 of 111
Download to read offline
Traversal
  郭至軒(KuoE0)
 KuoE0.tw@gmail.com
      KuoE0.ch
Attribution-ShareAlike 3.0 Unported
           (CC BY-SA 3.0)

  http://creativecommons.org/licenses/by-sa/3.0/

             Latest update: Mar 13, 2013
Graph
Traversal
Traversal
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS (Depth-First Search)
DFS & stack
    1


    2


3       4


5       6
DFS & stack
    1


    2


3       4


5       6     1
DFS & stack
    1


    2


3       4
              2
5       6     1
DFS & stack
    1


    2


3       4     3
              2
5       6     1
DFS & stack
    1


    2
              5
3       4     3
              2
5       6     1
DFS & stack
    1


    2


3       4     3
              2
5       6     1
DFS & stack
    1


    2


3       4
              2
5       6     1
DFS & stack
    1


    2


3       4     4
              2
5       6     1
DFS & stack
    1


    2
              6
3       4     4
              2
5       6     1
DFS & stack
    1


    2


3       4     4
              2
5       6     1
DFS & stack
    1


    2


3       4
              2
5       6     1
DFS & stack
    1


    2


3       4


5       6     1
DFS & stack
    1


    2


3       4


5       6
Source Code
// There are n nodes.
// g is an adjacent matrix.

void DFS( int now ) {
  if ( visited[ now ] == true )
    return;
  visited[ now ] = true;
  for ( int i = 0; i < n; ++i ) {
    if ( g[ now ][ i ] == true )
      DFS( i );
  }
  return;
}
Source Code
// There are n nodes.
// g is an adjacent matrix.

void DFS() {
   stack< int > stk;
   stk.push( 0 );
   while ( stk.empty() != true ) {
      int now = stk.top();
      visited[ now ] = true;
      bool noleaf = true;
      for ( int i = 0; i < n; ++i )
         if ( g[ now ][ i ] == true && visited[ i ] != true ) {
            stk.push( i );
            noleaf = false;
            break;
         }
      if ( noleaf == true )
         stk.pop();
   }
}
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS & queue
    1


    2


3       4


5       6
BFS & queue
    1       1


    2


3       4


5       6
BFS & queue
    1       1


    2


3       4


5       6
BFS & queue
    1       1   2


    2


3       4


5       6
BFS & queue
    1       2


    2


3       4


5       6
BFS & queue
    1       2


    2


3       4


5       6
BFS & queue
    1       2   3


    2


3       4


5       6
BFS & queue
    1       2   3   4


    2


3       4


5       6
BFS & queue
    1       3   4


    2


3       4


5       6
BFS & queue
    1       3   4


    2


3       4


5       6
BFS & queue
    1       3   4   5


    2


3       4


5       6
BFS & queue
    1       4   5


    2


3       4


5       6
BFS & queue
    1       4   5


    2


3       4


5       6
BFS & queue
    1       4   5   6


    2


3       4


5       6
BFS & queue
    1       5   6


    2


3       4


5       6
BFS & queue
    1       5   6


    2


3       4


5       6
BFS & queue
    1       6


    2


3       4


5       6
BFS & queue
    1       6


    2


3       4


5       6
BFS & queue
    1


    2


3       4


5       6
Source Code
// There are n nodes.
// g is an adjacent matrix.

void BFS() {
  queue< int > que;
  que.push( 0 );
  while ( que.empty() != true ) {
    int now = que.front();
    que.pop();
    visited[ now ] = true;
    for ( int i = 0; i < n; ++i )
      if ( g[ now ][ i ] == true )
        que.push( i );
  }
}
Time Complexity

 DFS      O(V+E)



 BFS      O(V+E)



         V: the number of nodes
         E: the number of edges
老鼠走迷宮 (DFS)
老鼠走迷宮 (DFS)
老鼠走迷宮 (BFS)
老鼠走迷宮 (BFS)

1
老鼠走迷宮 (BFS)

1   2
老鼠走迷宮 (BFS)

1   2   3
老鼠走迷宮 (BFS)

1   2   3   4
老鼠走迷宮 (BFS)

1   2   3   4   5

            5
老鼠走迷宮 (BFS)

1   2   3   4   5   6

            5

            6
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7

            5

        7   6   7
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7

            5           8

        7   6   7

        8
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7

            5           8   9

        7   6   7       9

    9   8
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7

            5           8    9   10

        7   6   7       9

    9   8               10
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11

            5           8   9   10

        7   6   7       9

    9   8               10 11
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12

            5           8   9   10

        7   6   7       9

    9   8               10 11 12
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13

            5           8   9   10

        7   6   7       9

    9   8               10 11 12     13
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
老鼠走迷宮 (BFS)

1   2   3   4   5   6   7       11 12 13 14

            5           8   9   10      14

        7   6   7       9

    9   8               10 11 12     13 14 15 16
Practice Now
[UVa] 352 - The Seasonal War
352 - The Seasonal War
      Sample Input       Sample Output
 6                   Image number 1 contains 3 war eagles.
 100100              Image number 2 contains 6 war eagles.
 001010
 000000
 110000
 111000
 010100
 8
 01100101
 01000001
 00011000
 00000010
 11000011
 10100010
 10000001
 01100000
352 - The Seasonal War
352 - The Seasonal War
352 - The Seasonal War
352 - The Seasonal War
Source Code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
char map[ 30 ][ 30 ];
bool visited[ 30 ][ 30 ];
int step[ 8 ][ 2 ] = { { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, 1 },
{ 0, -1 }, { -1, 1 }, { -1, 0 }, { -1, -1 } };

void DFS( int r, int c );

int main() {
   int t = 0;
   while ( scanf( “%d”, &n ) != EOF ) {
      int ret = 0;
      for ( int i = 0; i < n; ++i )
         scanf( “%s”, map[ i ] );

     memeset( visited, 0, sizeof( visited ) );
Source Code
     for ( int i = 0; i < n; ++i )
        for ( int j = 0; j < n; ++j )
           if ( map[ i ][ j ] == ‘1’ && visited[ i ][ j ] != true ) {
              DFS( i, j );
              ++ret;
           }
     printf( “Image number %d contains %d war eagles.n”, ++t, ret );
  }
  return 0;
}
void DFS( int r, int c ) {
   if ( visited[ r ][ c ] == true )
      return;
   visited[ r ][ c ] = true;
   for ( int i = 0; i < 8; ++i ) {
      int r2 = r + step[ i ][ 0 ], c2 = c + step[ i ][ 1 ];
      if ( r2 >= 0 && r2 < n && c2 >= 0 && c2 < n && map[ r2 ][ c2 ]
      == ‘1’ )
         DFS( r2, c2 );
   }
}
Practice Now
[UVa] 260 - Il Gioco dell'X
Thank You for Your Listening.

More Related Content

More from Chih-Hsuan Kuo

在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Chih-Hsuan Kuo
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoChih-Hsuan Kuo
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSChih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-upChih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 

More from Chih-Hsuan Kuo (20)

在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
 
[ACM-ICPC] About I/O
[ACM-ICPC] About I/O[ACM-ICPC] About I/O
[ACM-ICPC] About I/O
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 

[ACM-ICPC] Traversal