Lecture17 Graphs
Lecture17 Graphs
• Graph problems
A E
• Two ways of implementing graphs F
• BFS and DFS, their complexity
1
Notation Adjacency relation
• Set V of vertices (nodes) • Node B is adjacent to A if there is an edge from A to B.
• Set E of edges (E ⊆ V × V)
A B
Example: B C
2
More graph problems How to implement a graph
• Topological sort (finding a linear sequence of vertices As with lists, there are several approaches, e.g.:
which agrees with the direction of edges in the graph, e.g.,
for scheduling tasks in a project) • using a static indexed data structure
• Minimal spanning tree (deleting as many edges in a graph • using a dynamic data structure
as possible, so that all vertices are still connected by
shortest possible edges, e.g., in network routing or circuit
design.)
Static implementation:Adjacency
Example
Matrix
• Store node in the array: each node is associated with an B 0 1 2 3
integer (array index)
0 0 1 1 0
• Represent information about the edges using a two
dimensional array, where A D 1 0 0 0 1
2 0 0 0 1
array[i][j] == 1
3 0 0 0 0
iff there is an edge from node with index i to the node C
with index j. A B C D node indices adjacency
0 1 2 3 matrix
3
Disadvantages of adjacency
Adjacency List
matrices
• Sparse graphs with few edges for number of vertices result • For every vertex, keep a list of adjacent vertices.
in many zero entries in adjacency matrix—this wastes • Keep a list of vertices, or keep vertices in a HashMap as
space and makes many algorithms less efficient (e.g., to keys and lists of adjacent vertices as values.
find nodes adjacent to a given node, we have to iterate
through the whole row even if there are few 1s there).
4
Breadth first search BFS starting from A:
5
Modification of depth first search Tracing modified DFS from A
Modified DFS starting from v: S = {}
all vertices coloured white
C S=A
create a stack S
colour v grey and push v onto S B B
while S is non-empty A S=A
peek at the top u of S E C
D
if u has a grey neighbour, there is a B
cycle S=A
else if u has a white neighbour w,
colour w grey and push it onto S B
pop: S=A
else colour u black and pop S
6
Space Complexity of BFS and
Pseudocode for DFS DFS
s.marked = true; • Need a queue/stack of size |V| (the number of vertices).
Stack S = new Stack(); Space complexity O(V).
S.push(s);
while(! S.isempty()){
v = S.peek();
u = firstUnmarkedAdj(v);
if (u == null) S.pop();
else {
u.marked = true;
S.push(u);
}
}
v2
7
Time complexity of BFS Time complexity of BFS
Adjacency lists: Adjacency lists:
V E V E
v1 v1
v0: {v1,v2} v0: {v1,v2}
v1: {v3} dequeue v1; mark, v1: {v3}
v0 v3 v0 v3
enqueue v3 v2: {v3} dequeue v2, check
v2: {v3} its adjacency list (v3
v3: {} already marked)
v2 v2 v3: {}
Complexity of breadth-first
search Complexity of depth-first search
• Assume an adjacency list representation, V is the number • Each vertex is pushed on the stack and popped at most
of vertices, E the number of edges. once.
• Each vertex is enqueued and dequeued at most once.
• For every vertex we check what the next unvisited
• Scanning for all adjacent vertices takes O(|E|) time, since neighbour is.
sum of lengths of adjacency lists is |E|.
• Gives a O(|V|+|E|) time complexity. • In our implementation, we traverse the adjacency list only
once. This gives O(|V|+|E|) again.