그래프의 탐색 방법은 깊이 우선 탐색(DFS), 너비 우선 탐색(BFS) 이 있다.
각각에 대해서는 위키피디아 링크 를 참조…
구현 >>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# 인접 행렬로 구현된 그래프를 참조한다
require 'graph'
# 너비 우선 탐색에서 큐를 참조한다
require 'circular_queue'
class Graph
# 깊이 우선 탐색
def depth_first_search( start_vertex_name = nil )
unless start_vertex_name == nil
start_index = find_vertex(start_vertex_name)
else
start_index = 0
end
if @max_vertex_number < 0 || start_index < 0
puts
return
end
visited = []
(0..@max_vertex_number).each {|i| visited[i] = false}
depth_first_search_recursive(visited, start_index)
puts
end
# 너비 우선 탐색
def breadth_first_search( start_vertex_name = nil )
unless start_vertex_name == nil
start_index = find_vertex(start_vertex_name)
else
start_index = 0
end
if @max_vertex_number < 0 || start_index < 0
puts
return
end
visited = []
(0..@max_vertex_number).each {|i| visited[i] = false}
queue = CircularQueue.new(@max_vertex_number + 1)
current_visit = start_index
visited[current_visit] = true
print "-> " + @vertex_set[current_visit].to_s
loop do
(0..@adjacent_matrix[current_visit].length).each do |i|
if 1 == @adjacent_matrix[current_visit][i] && false == visited[i]
visited[i] = true
print "-> " + @vertex_set[i].to_s
queue.enqueue(i)
end
end
break if true == queue.is_empty
current_visit = queue.dequeue
end
puts
end
private
# 스택 부분을 재귀 구조가 알아서 처리하므로 따로 스택을 push / pop 할 필요 없음
def depth_first_search_recursive(visited_list, current_visit)
visited_list[current_visit] = true
print "-> " + @vertex_set[current_visit].to_s
(0..@adjacent_matrix[current_visit].length).each do |i|
if 1 == @adjacent_matrix[current_visit][i] && false == visited_list[i]
depth_first_search_recursive(visited_list, i)
end
end
# 방문하지 않은 정점이 없다면 스택을 pop 하여 구한 가장 마지막 방문 정점을
# v로 설정하고 다시 (2) 를 수행한다
end
end
graph = Graph.new(false)
graph.add_vertex('A')
graph.add_vertex('B')
graph.add_vertex('C')
graph.add_vertex('D')
graph.add_vertex('E')
graph.add_vertex('F')
graph.add_vertex('G')
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'D')
graph.add_edge('B', 'E')
graph.add_edge('C', 'E')
graph.add_edge('D', 'G')
graph.add_edge('E', 'G')
graph.add_edge('F', 'G')
puts "깊이 우선 탐색(BFS) >> "
graph.depth_first_search
puts
puts "너비 우선 탐색(DFS) >> "
graph.breadth_first_search
|
결과 >>
깊이 우선 탐색(BFS) >>
-> A-> B-> D-> G-> E-> C-> F
너비 우선 탐색(DFS) >>
-> A-> B-> C-> D-> E-> G-> F
'Dev.Programming > DS/Algorithm' 카테고리의 다른 글
| 퀵 정렬 - in Ruby (0) | 2009/03/17 |
|---|---|
| 선택 정렬 & 버블 정렬 in Ruby (2) | 2009/03/17 |
| 그래프 탐색(깊이 우선 탐색, 너비 우선 탐색) - in Ruby (0) | 2009/03/16 |
| 인접 리스트로 구현한 Graph - in Ruby (2) | 2009/03/16 |
| 인접 행렬로 구현한 Graph - in Ruby (0) | 2009/03/16 |
| Heap - in Ruby (0) | 2009/03/05 |


