Download presentation
1
Graph theory Prof Amir Geva Eitan Netzer
2
G=(V,E) V – Vertex E – Edges
3
Directed Graph
4
Undirected Graph
5
Weighted Graph
6
Representation
7
BFS Shortest distance from vertex s to each vertex v
Work on directed and undirected First all are white Done with vertex black Else grey Time and space
8
BFS BFS(G,s) for each vertex u∈V[G]-{s} do color[u]<-white
d[u]<-∞ π [u]<-NULL color[s]<-gray d[s]<-0 π [s]<-NULL Q<-{s} while Q≠ φ do u<-head[Q] for each v∈Adj[u] do if color[v] = White then color[v]<- Gray d[v]<- d[u]+1 π [v]<- u Enqueue(Q,v) Dequeue(Q) color[u]<- Black
9
BFS
10
DFS Search all vertexes starting at vertex s Not have to be minimal
Time Space
11
DFS DFS(G,s) for each vertex u∈V[G] do color[u]<-white
π [u]<-NULL time <- 0 do if color[u] = white then DFS-Visit(u) DFS-Visit(u) color[u]<-Gray d[u] <- time <- time +1 for each vertex v∈Adj[u] do if color[v] =white then π [v]<-u DFS-Visit(v) color[u] = Black f[u] <- time <- time +1
12
Improve weight Relax(u,v,w) if d[v]>d[u] + w(u,v)
then d[v]<- d[u] + w(u,v) π [v]<- u
13
Dijkstra's algorithm Find shortest path from vertex s to all other vertexes. Work on weighted directed and undirected. But non negative weights List or array Heap
14
Dijkstra Dijkstra(G,w,s) for each vertex u∈V[G] do d[u]<-∞
π [u]<-NULL d[s]<-0 S <- φ Q<-V[G] while Q≠ φ do u<- extract-min(Q) S<-S ∪{u} for each vertex v∈Adj[u] , Q do Relax(u,v,w)
16
Negative example
17
Bellman–Ford algorithm
Find shortest path from vertex s to all other vertexes. Work on weighted directed can handle negative weights
18
Bellman–Ford Bellman-ford(G,w,s) for each vertex u∈V[G] do d[u]<-∞
π [u]<-NULL d[s]<-0 for i <- 1 to |V(G)|-1 do for each edge (u,v)∈E[G] do Relax(u,v,w) for each edge (u,v)∈E[G] do if d[v]>d[u]+w(u,v) then return FALSE return TRUE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.