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/             L...
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    23       45       6
DFS & stack    1    23       45       6     1
DFS & stack    1    23       4              25       6     1
DFS & stack    1    23       4     3              25       6     1
DFS & stack    1    2              53       4     3              25       6     1
DFS & stack    1    23       4     3              25       6     1
DFS & stack    1    23       4              25       6     1
DFS & stack    1    23       4     4              25       6     1
DFS & stack    1    2              63       4     4              25       6     1
DFS & stack    1    23       4     4              25       6     1
DFS & stack    1    23       4              25       6     1
DFS & stack    1    23       45       6     1
DFS & stack    1    23       45       6
Source Code// There are n nodes.// g is an adjacent matrix.void DFS( int now ) {  if ( visited[ now ] == true )    return;...
Source Code// There are n nodes.// g is an adjacent matrix.void DFS() {   stack< int > stk;   stk.push( 0 );   while ( stk...
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS (Breadth-First Search)
BFS & queue    1    23       45       6
BFS & queue    1       1    23       45       6
BFS & queue    1       1    23       45       6
BFS & queue    1       1   2    23       45       6
BFS & queue    1       2    23       45       6
BFS & queue    1       2    23       45       6
BFS & queue    1       2   3    23       45       6
BFS & queue    1       2   3   4    23       45       6
BFS & queue    1       3   4    23       45       6
BFS & queue    1       3   4    23       45       6
BFS & queue    1       3   4   5    23       45       6
BFS & queue    1       4   5    23       45       6
BFS & queue    1       4   5    23       45       6
BFS & queue    1       4   5   6    23       45       6
BFS & queue    1       5   6    23       45       6
BFS & queue    1       5   6    23       45       6
BFS & queue    1       6    23       45       6
BFS & queue    1       6    23       45       6
BFS & queue    1    23       45       6
Source Code// There are n nodes.// g is an adjacent matrix.void BFS() {  queue< int > que;  que.push( 0 );  while ( que.em...
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         ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12            5           8   9   10        7   6   7       9    9   8      ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13            5           8   9   10        7   6   7       9    9   8   ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
老鼠走迷宮 (BFS)1   2   3   4   5   6   7       11 12 13 14            5           8   9   10      14        7   6   7       9 ...
Practice Now[UVa] 352 - The Seasonal War
352 - The Seasonal War      Sample Input       Sample Output 6                   Image number 1 contains 3 war eagles. 100...
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 visit...
Source Code     for ( int i = 0; i < n; ++i )        for ( int j = 0; j < n; ++j )           if ( map[ i ][ j ] == ‘1’ && ...
Practice Now[UVa] 260 - Il Gioco dellX
Thank You for Your Listening.
Upcoming SlideShare
Loading in...5
×

[ACM-ICPC] Traversal

326

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
326
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "[ACM-ICPC] Traversal"

  1. 1. Traversal 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  2. 2. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ Latest update: Mar 13, 2013
  3. 3. Graph
  4. 4. Traversal
  5. 5. Traversal
  6. 6. DFS (Depth-First Search)
  7. 7. DFS (Depth-First Search)
  8. 8. DFS (Depth-First Search)
  9. 9. DFS (Depth-First Search)
  10. 10. DFS (Depth-First Search)
  11. 11. DFS (Depth-First Search)
  12. 12. DFS (Depth-First Search)
  13. 13. DFS (Depth-First Search)
  14. 14. DFS (Depth-First Search)
  15. 15. DFS (Depth-First Search)
  16. 16. DFS (Depth-First Search)
  17. 17. DFS (Depth-First Search)
  18. 18. DFS (Depth-First Search)
  19. 19. DFS (Depth-First Search)
  20. 20. DFS (Depth-First Search)
  21. 21. DFS (Depth-First Search)
  22. 22. DFS & stack 1 23 45 6
  23. 23. DFS & stack 1 23 45 6 1
  24. 24. DFS & stack 1 23 4 25 6 1
  25. 25. DFS & stack 1 23 4 3 25 6 1
  26. 26. DFS & stack 1 2 53 4 3 25 6 1
  27. 27. DFS & stack 1 23 4 3 25 6 1
  28. 28. DFS & stack 1 23 4 25 6 1
  29. 29. DFS & stack 1 23 4 4 25 6 1
  30. 30. DFS & stack 1 2 63 4 4 25 6 1
  31. 31. DFS & stack 1 23 4 4 25 6 1
  32. 32. DFS & stack 1 23 4 25 6 1
  33. 33. DFS & stack 1 23 45 6 1
  34. 34. DFS & stack 1 23 45 6
  35. 35. 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;}
  36. 36. 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(); }}
  37. 37. BFS (Breadth-First Search)
  38. 38. BFS (Breadth-First Search)
  39. 39. BFS (Breadth-First Search)
  40. 40. BFS (Breadth-First Search)
  41. 41. BFS (Breadth-First Search)
  42. 42. BFS & queue 1 23 45 6
  43. 43. BFS & queue 1 1 23 45 6
  44. 44. BFS & queue 1 1 23 45 6
  45. 45. BFS & queue 1 1 2 23 45 6
  46. 46. BFS & queue 1 2 23 45 6
  47. 47. BFS & queue 1 2 23 45 6
  48. 48. BFS & queue 1 2 3 23 45 6
  49. 49. BFS & queue 1 2 3 4 23 45 6
  50. 50. BFS & queue 1 3 4 23 45 6
  51. 51. BFS & queue 1 3 4 23 45 6
  52. 52. BFS & queue 1 3 4 5 23 45 6
  53. 53. BFS & queue 1 4 5 23 45 6
  54. 54. BFS & queue 1 4 5 23 45 6
  55. 55. BFS & queue 1 4 5 6 23 45 6
  56. 56. BFS & queue 1 5 6 23 45 6
  57. 57. BFS & queue 1 5 6 23 45 6
  58. 58. BFS & queue 1 6 23 45 6
  59. 59. BFS & queue 1 6 23 45 6
  60. 60. BFS & queue 1 23 45 6
  61. 61. 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 ); }}
  62. 62. Time Complexity DFS O(V+E) BFS O(V+E) V: the number of nodes E: the number of edges
  63. 63. 老鼠走迷宮 (DFS)
  64. 64. 老鼠走迷宮 (DFS)
  65. 65. 老鼠走迷宮 (BFS)
  66. 66. 老鼠走迷宮 (BFS)1
  67. 67. 老鼠走迷宮 (BFS)1 2
  68. 68. 老鼠走迷宮 (BFS)1 2 3
  69. 69. 老鼠走迷宮 (BFS)1 2 3 4
  70. 70. 老鼠走迷宮 (BFS)1 2 3 4 5 5
  71. 71. 老鼠走迷宮 (BFS)1 2 3 4 5 6 5 6
  72. 72. 老鼠走迷宮 (BFS)1 2 3 4 5 6 7 5 7 6 7
  73. 73. 老鼠走迷宮 (BFS)1 2 3 4 5 6 7 5 8 7 6 7 8
  74. 74. 老鼠走迷宮 (BFS)1 2 3 4 5 6 7 5 8 9 7 6 7 9 9 8
  75. 75. 老鼠走迷宮 (BFS)1 2 3 4 5 6 7 5 8 9 10 7 6 7 9 9 8 10
  76. 76. 老鼠走迷宮 (BFS)1 2 3 4 5 6 7 11 5 8 9 10 7 6 7 9 9 8 10 11
  77. 77. 老鼠走迷宮 (BFS)1 2 3 4 5 6 7 11 12 5 8 9 10 7 6 7 9 9 8 10 11 12
  78. 78. 老鼠走迷宮 (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
  79. 79. 老鼠走迷宮 (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
  80. 80. 老鼠走迷宮 (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
  81. 81. 老鼠走迷宮 (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
  82. 82. 老鼠走迷宮 (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
  83. 83. 老鼠走迷宮 (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
  84. 84. 老鼠走迷宮 (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
  85. 85. 老鼠走迷宮 (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
  86. 86. 老鼠走迷宮 (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
  87. 87. 老鼠走迷宮 (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
  88. 88. 老鼠走迷宮 (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
  89. 89. 老鼠走迷宮 (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
  90. 90. 老鼠走迷宮 (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
  91. 91. 老鼠走迷宮 (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
  92. 92. 老鼠走迷宮 (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
  93. 93. 老鼠走迷宮 (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
  94. 94. 老鼠走迷宮 (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
  95. 95. 老鼠走迷宮 (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
  96. 96. 老鼠走迷宮 (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
  97. 97. 老鼠走迷宮 (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
  98. 98. 老鼠走迷宮 (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
  99. 99. 老鼠走迷宮 (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
  100. 100. 老鼠走迷宮 (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
  101. 101. 老鼠走迷宮 (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
  102. 102. Practice Now[UVa] 352 - The Seasonal War
  103. 103. 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
  104. 104. 352 - The Seasonal War
  105. 105. 352 - The Seasonal War
  106. 106. 352 - The Seasonal War
  107. 107. 352 - The Seasonal War
  108. 108. 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 ) );
  109. 109. 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 ); }}
  110. 110. Practice Now[UVa] 260 - Il Gioco dellX
  111. 111. Thank You for Your Listening.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×