-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdbfs.rb
148 lines (131 loc) · 2.72 KB
/
tdbfs.rb
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class Node
attr_accessor :id, :state, :siblings
def initialize(id, state, siblings)
@id = id
@state = state # for symbols :fresh / :opened / :closed
@siblings = siblings # list of siblings = list of references to node objects!!!
end
def to_s
return "#{id}-#{state}"
end
end
class Graph
attr_accessor :nodes
def initialize(nodes)
@nodes = nodes
end
public
def getNode(id)
for node in nodes
if node.id == id
return node
end
end
end
def makeNodesFresh()
for node in nodes
node.state = :fresh
end
end
def to_s
result = ""
for node in nodes
result += "#{node.to_s} "
end
return result
end
end
# dfs - recursive
def dfs1(graph, node)
result = []
dfs(graph, node, result)
graph.makeNodesFresh
return result.join(" ")
end
def dfs(graph, node, result)
node.state = :opened
result << node.id
for sibling in node.siblings
if(sibling.state == :fresh)
dfs(graph, sibling, result)
end
end
node.state = :closed
end
# dfs - while loop
def dfs2(graph, root)
stack = [root]
result = []
while node = stack.pop()
if (node.state == :fresh)
result << node.id
end
node.state = :opened
for sibling in node.siblings.reverse
if (sibling.state == :fresh)
stack.push(sibling)
end
end
end
graph.makeNodesFresh
return result.join(" ")
end
def bfs(graph, root)
queue = [root]
root.state = :opened
result = []
while node = queue.shift()
result << node.id
for sibling in node.siblings
if sibling.state == :fresh
queue.push(sibling)
sibling.state = :opened
end
end
end
graph.makeNodesFresh
return result.join(" ")
end
# --- main ----
graphSum = gets.to_i
graphCount = 0
graphList = []
while graphCount < graphSum
graphNodes = []
graphCount += 1
puts "graph #{graphCount}"
vertexCount = gets.to_i
verticesHash = Hash.new
1.upto(vertexCount) { |j|
splitted = gets.chomp.split(" ")
nodeSiblings = Array.new
2.upto(splitted[1].to_i+1) { |i|
if verticesHash[splitted[i].to_i] == nil
node = Node.new(splitted[i].to_i, :fresh, nil)
nodeSiblings << node
graphNodes << node
verticesHash.store(splitted[i].to_i, node)
else
nodeSiblings << verticesHash[splitted[i].to_i]
end
}
if verticesHash[j] == nil
node = Node.new(j, :fresh, nodeSiblings)
verticesHash.store(j, node)
graphNodes << node
else
verticesHash[j].siblings = nodeSiblings
end
}
graphList << Graph.new(graphNodes)
until (splitted = gets.chomp.split(" ")) == ["0","0"]
graph = graphList[graphCount-1]
rootNode = graph.getNode(splitted[0].to_i)
if splitted[1].to_i == 1
puts bfs(graph, rootNode)
else
puts dfs1(graph, rootNode) # recursive
#puts dfs2(graph, rootNode) # while loop
end
end
end