-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-graph.rb
More file actions
494 lines (388 loc) · 12.2 KB
/
12-graph.rb
File metadata and controls
494 lines (388 loc) · 12.2 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# ========================================
# GRAPHS IN RUBY
# ========================================
# A graph is a non-linear data structure consisting of vertices (nodes)
# and edges that connect them.
puts "=" * 50
puts "GRAPHS IN RUBY"
puts "=" * 50
# ========================================
# 1. ADJACENCY LIST REPRESENTATION
# ========================================
class Graph
def initialize(directed = false)
@adjacency_list = Hash.new { |h, k| h[k] = [] }
@directed = directed
end
def add_vertex(vertex)
@adjacency_list[vertex] ||= []
end
def add_edge(vertex1, vertex2, weight = nil)
@adjacency_list[vertex1] << (weight ? { vertex: vertex2, weight: weight } : vertex2)
@adjacency_list[vertex2] << (weight ? { vertex: vertex1, weight: weight } : vertex1) unless @directed
end
def vertices
@adjacency_list.keys
end
def edges
all_edges = []
@adjacency_list.each do |vertex, neighbors|
neighbors.each do |neighbor|
edge = [vertex, neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor]
all_edges << edge unless @directed && all_edges.include?(edge.reverse)
end
end
all_edges
end
def neighbors(vertex)
@adjacency_list[vertex]
end
def display
puts "\nGraph (#{@directed ? 'Directed' : 'Undirected'}):"
@adjacency_list.each do |vertex, neighbors|
neighbors_str = neighbors.map do |n|
n.is_a?(Hash) ? "#{n[:vertex]}(#{n[:weight]})" : n.to_s
end.join(', ')
puts " #{vertex} -> [#{neighbors_str}]"
end
end
end
puts "\n1. Adjacency List Representation:"
graph = Graph.new
['A', 'B', 'C', 'D'].each { |v| graph.add_vertex(v) }
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'D')
graph.add_edge('C', 'D')
graph.display
# ========================================
# 2. WEIGHTED GRAPH
# ========================================
puts "\n2. Weighted Graph:"
weighted_graph = Graph.new
['A', 'B', 'C', 'D'].each { |v| weighted_graph.add_vertex(v) }
weighted_graph.add_edge('A', 'B', 4)
weighted_graph.add_edge('A', 'C', 2)
weighted_graph.add_edge('B', 'D', 5)
weighted_graph.add_edge('C', 'D', 3)
weighted_graph.display
# ========================================
# 3. DEPTH-FIRST SEARCH (DFS)
# ========================================
class Graph
def dfs(start, visited = Set.new, &block)
return if visited.include?(start)
visited.add(start)
block.call(start) if block_given?
neighbors(start).each do |neighbor|
vertex = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
dfs(vertex, visited, &block)
end
visited
end
def dfs_iterative(start)
visited = Set.new
stack = [start]
result = []
while !stack.empty?
vertex = stack.pop
next if visited.include?(vertex)
visited.add(vertex)
result << vertex
neighbors(vertex).reverse_each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
stack.push(v) unless visited.include?(v)
end
end
result
end
end
puts "\n3. Depth-First Search:"
puts "DFS Recursive:"
graph.dfs('A') { |v| print "#{v} " }
puts
puts "DFS Iterative:"
puts graph.dfs_iterative('A').join(' ')
# ========================================
# 4. BREADTH-FIRST SEARCH (BFS)
# ========================================
class Graph
def bfs(start)
visited = Set.new
queue = [start]
result = []
while !queue.empty?
vertex = queue.shift
next if visited.include?(vertex)
visited.add(vertex)
result << vertex
neighbors(vertex).each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
queue.push(v) unless visited.include?(v)
end
end
result
end
end
puts "\n4. Breadth-First Search:"
puts "BFS: #{graph.bfs('A').join(' ')}"
# ========================================
# 5. PATH FINDING
# ========================================
class Graph
def has_path?(start, end_vertex, visited = Set.new)
return true if start == end_vertex
return false if visited.include?(start)
visited.add(start)
neighbors(start).each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
return true if has_path?(v, end_vertex, visited)
end
false
end
def find_path(start, end_vertex)
queue = [[start, [start]]]
visited = Set.new
while !queue.empty?
vertex, path = queue.shift
return path if vertex == end_vertex
next if visited.include?(vertex)
visited.add(vertex)
neighbors(vertex).each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
queue.push([v, path + [v]]) unless visited.include?(v)
end
end
nil
end
def all_paths(start, end_vertex, path = [], all_paths = [])
path = path + [start]
if start == end_vertex
all_paths << path
return all_paths
end
neighbors(start).each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
all_paths(v, end_vertex, path, all_paths) unless path.include?(v)
end
all_paths
end
end
puts "\n5. Path Finding:"
puts "Has path from A to D? #{graph.has_path?('A', 'D')}"
puts "Path from A to D: #{graph.find_path('A', 'D')}"
puts "All paths from A to D:"
graph.all_paths('A', 'D').each { |path| puts " #{path.join(' -> ')}" }
# ========================================
# 6. CYCLE DETECTION
# ========================================
class Graph
def has_cycle?
visited = Set.new
rec_stack = Set.new
vertices.each do |vertex|
return true if has_cycle_util?(vertex, visited, rec_stack)
end
false
end
private
def has_cycle_util?(vertex, visited, rec_stack)
return true if rec_stack.include?(vertex)
return false if visited.include?(vertex)
visited.add(vertex)
rec_stack.add(vertex)
neighbors(vertex).each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
return true if has_cycle_util?(v, visited, rec_stack)
end
rec_stack.delete(vertex)
false
end
end
puts "\n6. Cycle Detection:"
cyclic_graph = Graph.new(true)
['A', 'B', 'C'].each { |v| cyclic_graph.add_vertex(v) }
cyclic_graph.add_edge('A', 'B')
cyclic_graph.add_edge('B', 'C')
cyclic_graph.add_edge('C', 'A')
puts "Cyclic graph has cycle? #{cyclic_graph.has_cycle?}"
acyclic_graph = Graph.new(true)
['A', 'B', 'C'].each { |v| acyclic_graph.add_vertex(v) }
acyclic_graph.add_edge('A', 'B')
acyclic_graph.add_edge('A', 'C')
puts "Acyclic graph has cycle? #{acyclic_graph.has_cycle?}"
# ========================================
# 7. TOPOLOGICAL SORT
# ========================================
class Graph
def topological_sort
visited = Set.new
stack = []
vertices.each do |vertex|
topological_sort_util(vertex, visited, stack) unless visited.include?(vertex)
end
stack.reverse
end
private
def topological_sort_util(vertex, visited, stack)
visited.add(vertex)
neighbors(vertex).each do |neighbor|
v = neighbor.is_a?(Hash) ? neighbor[:vertex] : neighbor
topological_sort_util(v, visited, stack) unless visited.include?(v)
end
stack.push(vertex)
end
end
puts "\n7. Topological Sort:"
dag = Graph.new(true)
['A', 'B', 'C', 'D', 'E'].each { |v| dag.add_vertex(v) }
dag.add_edge('A', 'C')
dag.add_edge('B', 'C')
dag.add_edge('B', 'D')
dag.add_edge('C', 'E')
dag.add_edge('D', 'E')
dag.display
puts "Topological order: #{dag.topological_sort.join(' -> ')}"
# ========================================
# 8. DIJKSTRA'S SHORTEST PATH
# ========================================
class Graph
def dijkstra(start)
distances = Hash.new(Float::INFINITY)
distances[start] = 0
previous = {}
unvisited = vertices.to_set
while !unvisited.empty?
current = unvisited.min_by { |v| distances[v] }
break if distances[current] == Float::INFINITY
unvisited.delete(current)
neighbors(current).each do |neighbor|
vertex = neighbor[:vertex]
weight = neighbor[:weight]
alt = distances[current] + weight
if alt < distances[vertex]
distances[vertex] = alt
previous[vertex] = current
end
end
end
{ distances: distances, previous: previous }
end
def shortest_path(start, end_vertex)
result = dijkstra(start)
path = []
current = end_vertex
while current
path.unshift(current)
current = result[:previous][current]
end
{ path: path, distance: result[:distances][end_vertex] }
end
end
puts "\n8. Dijkstra's Shortest Path:"
weighted_graph.display
result = weighted_graph.shortest_path('A', 'D')
puts "Shortest path from A to D: #{result[:path].join(' -> ')}"
puts "Distance: #{result[:distance]}"
# ========================================
# 9. CONNECTED COMPONENTS
# ========================================
class Graph
def connected_components
visited = Set.new
components = []
vertices.each do |vertex|
unless visited.include?(vertex)
component = []
dfs(vertex, visited) { |v| component << v }
components << component
end
end
components
end
def is_connected?
connected_components.length == 1
end
end
puts "\n9. Connected Components:"
disconnected = Graph.new
['A', 'B', 'C', 'D', 'E', 'F'].each { |v| disconnected.add_vertex(v) }
disconnected.add_edge('A', 'B')
disconnected.add_edge('B', 'C')
disconnected.add_edge('D', 'E')
disconnected.display
puts "Connected components:"
disconnected.connected_components.each_with_index do |component, i|
puts " Component #{i + 1}: #{component.join(', ')}"
end
puts "Is connected? #{disconnected.is_connected?}"
# ========================================
# 10. PRACTICAL APPLICATIONS
# ========================================
puts "\n10. Practical Applications:"
# Application 1: Social Network
puts "\nSocial Network:"
social = Graph.new
['Alice', 'Bob', 'Charlie', 'David', 'Eve'].each { |user| social.add_vertex(user) }
social.add_edge('Alice', 'Bob')
social.add_edge('Alice', 'Charlie')
social.add_edge('Bob', 'David')
social.add_edge('Charlie', 'Eve')
social.add_edge('David', 'Eve')
puts "Friends of Alice: #{social.neighbors('Alice').join(', ')}"
puts "Path from Alice to Eve: #{social.find_path('Alice', 'Eve').join(' -> ')}"
# Application 2: Course Prerequisites
puts "\nCourse Prerequisites:"
courses = Graph.new(true)
['Intro CS', 'Data Structures', 'Algorithms', 'AI', 'ML'].each { |c| courses.add_vertex(c) }
courses.add_edge('Intro CS', 'Data Structures')
courses.add_edge('Data Structures', 'Algorithms')
courses.add_edge('Algorithms', 'AI')
courses.add_edge('AI', 'ML')
puts "Course order: #{courses.topological_sort.join(' -> ')}"
# Application 3: City Network
puts "\nCity Network (Shortest Path):"
cities = Graph.new
['NYC', 'Boston', 'Philadelphia', 'Washington DC'].each { |c| cities.add_vertex(c) }
cities.add_edge('NYC', 'Boston', 215)
cities.add_edge('NYC', 'Philadelphia', 95)
cities.add_edge('Philadelphia', 'Washington DC', 140)
cities.add_edge('Boston', 'Washington DC', 440)
result = cities.shortest_path('NYC', 'Washington DC')
puts "Shortest route: #{result[:path].join(' -> ')}"
puts "Total distance: #{result[:distance]} miles"
# ========================================
# TIME COMPLEXITY ANALYSIS
# ========================================
puts "\n" + "=" * 50
puts "TIME COMPLEXITY ANALYSIS"
puts "=" * 50
puts "Adjacency List:"
puts " Add vertex: O(1)"
puts " Add edge: O(1)"
puts " Remove vertex: O(V + E)"
puts " Remove edge: O(E)"
puts " Query edge: O(V)"
puts " Space: O(V + E)"
puts "\nTraversal:"
puts " DFS: O(V + E)"
puts " BFS: O(V + E)"
puts "\nAlgorithms:"
puts " Dijkstra: O((V + E) log V)"
puts " Topological Sort: O(V + E)"
puts " Cycle Detection: O(V + E)"
puts "=" * 50
# ========================================
# PRACTICE PROBLEMS
# ========================================
puts "\nPRACTICE PROBLEMS:"
puts "1. Number of islands (2D grid)"
puts "2. Clone a graph"
puts "3. Course schedule (detect cycle)"
puts "4. Word ladder transformation"
puts "5. Network delay time"
puts "6. Critical connections (bridges)"
puts "7. Minimum spanning tree (Kruskal/Prim)"
puts "8. Traveling salesman problem"
puts "9. Graph coloring"
puts "10. Strongly connected components"