CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Week 4 Kullmann Graphs and directed graphs Elementary Graph - - PowerPoint PPT Presentation
Week 4 Kullmann Graphs and directed graphs Elementary Graph - - PowerPoint PPT Presentation
CS 270 Algorithms Oliver Week 4 Kullmann Graphs and directed graphs Elementary Graph Algorithms Representing graphs Trees Breadth-first search Graphs and directed graphs 1 Representing graphs 2 Trees 3 Breadth-first search 4 CS
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
General remarks
We consider graphs, an important data structure. We consider the simplest graph-search algorithm, breadth-first search (BFS). We consider one application of BFS, the computation of shortest paths.
Reading from CLRS for week 4
Chapter 22, Sections 22.1, 22.2. Plus appendices B.4 “Graphs” B.5.1 “Free trees”
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Towards the mathematical notion of “graph”
Definition A graph G is a pair G = (V , E), where V is the set
- f vertices, while E is the set of (undirected) edges. An
(undirected) edge connecting vertices u, v is denoted by { u, v }. We require u = v here. An example is G = ({ 1, 2, 3, 4, 5 }, { { 1, 2 }, { 1, 3 }, { 2, 4 }, { 3, 4 } }) which is a graph with |V | = 5 vertices and |E| = 4 edges. A possible drawing is as follows: 1 2 3 4 5 We see that G has two “connected components”.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Directed graphs
A “graph” is synonymous with “undirected graph”. Now for a “directed graph” the edges are directed: Definition A directed graph (or digraph) G is a pair G = (V , E), where V is again the set of vertices, while E is the set of directed edges or arcs. A directed edge from vertex u to vertex v is denoted by (u, v). Again we require u = v here. An example is G = ({ 1, 2, 3, 4, 5 }, { (1, 2), (3, 1), (2, 4), (4, 3) }) which is a graph with |V | = 5 vertices and |E| = 4 edges. A possible drawing is as follows: 1
2
- 3
- 4
- 5
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Remarks on these fundamental notions
A loop is an edge or arc connecting a vertex with itself:
1 The notion of graph doesn’t allow loops; only the extension
- f “graphs with loops” allows them, but we do not consider
them here.
2 Also the notion of directed graph doesn’t allow loops
(different from the book we keep the symmetry between “graphs” and “directed graphs”); the extension of “directed graphs with loops” allows them, but again we do not consider them. For a graph G with n = |V | vertices we have |E| ≤ n 2
- = n(n − 1)
2 while for a directed graph G we have |E| ≤ n(n − 1).
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Representations of graphs
The above notions of graphs and digraphs yield mathematical
- bjects. These are the eternal platonic ideas, which have many
different representations in computers. There are two main ways to represent a graph G = (V , E): Adjacency lists: The graph is represented by a V -indexed array
- f linked lists, with each list containing the neighbours of a
single vertex. Adjacency matrix: The graph is represented by a |V | × |V | bit matrix where the Ai,j = 1 if and only if vertex i is adjacent to vertex j.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Adjacency list representation
1 2 4 6 3 5 7 1 2 2 1 3 4 3 2 4 5 4 2 3 5 6 7 5 3 4 7 6 4 7 4 5 For (undirected) graphs this representation uses two list elements for each edge (since both directions are present); for directed graphs this is only the case when antiparallel edges are present. The total space required for this representation is O(V + E) (that is, O(|V | + |E|)). This representation is suited to sparse graphs, where E is much less than V 2.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Adjacency matrix representation
1 2 4 6 3 5 7 A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 For (undirected) graphs the matrix is symmetric. The total space required for this representation is O(V 2). This representation is suited to dense graphs, where E is on the
- rder of V 2.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Comparing the two representations
Apart from the different space requirements needed for sparse and dense graphs, there are other criteria for deciding on which representation to use. The choice of representation will depend mostly on what questions are being asked. For example, consider the time required to decide the following questions using the two different representations: Is vertex v adjacent to vertex w in an undirected graph? What is the out-degree of a vertex v in a directed graph? What is the in-degree of a vertex v in a directed graph?
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
The notion of a tree
You might have seen already “rooted trees” (and you will see more of them in the following weeks) — trees are basically just like rooted trees, but without a designated root. Definition A tree is a graph T with at least one vertex, which is connected and does not have a cycle. Intuitively, a graph G has a cycle if there are two different vertices u, v in G such that there are (at least) two essentially different ways of getting from u to v. And thus going from u to v the one way, and from v to u the other way, we obtain the “round-trip” or “cycle”. We have the following fundamental characterisation of trees: Lemma 1 A graph G is a tree if and only if G is connected, |V | ≥ 1 and |E| = |V | − 1 holds. So trees realise minimal ways of connecting a set of vertices.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Examples
Here are two trees: 1 2 4
- 3
1 2 3 4
♣♣♣♣♣♣♣♣♣♣♣♣♣♣
8
- 7
6
◆◆◆◆◆◆◆◆◆◆◆◆◆◆
5
◆◆◆◆◆◆◆◆◆◆◆◆◆◆
And here is the smallest connected graph with at least one vertex, which is not a tree (the “triangle”): 1
❃ ❃ ❃ ❃ ❃ ❃ ❃ ❃
2 3 And here the smallest graph with at least one vertex (namely with two vertices), which is not a tree: 1 2
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Spanning trees
Definition Consider a connected graph G with at least one
- vertex. A spanning tree of G is a tree T with V (T) = V (G)
and E(T) ⊆ E(G). So spanning trees just leave out edges which are not needed to connect the whole graph. For example consider the graphs G1 = 1 2 3 4 , G2 = 1
❃ ❃ ❃ ❃ ❃ ❃ ❃ ❃
2 3 4 . G1 has 4 different spanning trees, while G2 has 4 + 4 = 8.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Computing spanning trees
We will see two algorithms, BFS and DFS, for computing spanning trees: In both cases actually rooted spanning trees are computed, that is, additionally a vertex is marked as “root”. When drawing such rooted spanning trees, one starts with the root (otherwise one could start with an arbitrary vertex!), going from the root to the leaves. For such rooted spanning trees, one typically speaks of nodes instead of vertices. Both algorithms compute additional data, besides the rooted spanning trees. The DFS version will be extended to compute a spanning forest: It can be applied to non-connected graphs, and computes a (rooted) spanning tree for each connected component.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Representing rooted trees
A rooted tree, that is, a tree together with a root T, will be represented by BFS and DFS as follows: Now there is a “direction” in the tree, either going from the root towards the leaves, or from the leaves towards the root. We obtain the usual notion of the children of a node (without a root, in a “pure tree”, there is no such thing). The leaves are the nodes without children. And we speak of the(!) parent of a node (note that every node can have at most one parent). The root is the only vertex without a parent. Now specifying the root for each non-root vertex is sufficient to represent the tree. This is done in BFS and DFS by an array π (like “parent”).
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Forests
A forest is a graph where every connected component is a tree: A connected component of a graph contains some vertex and precisely all vertices reachable from it. So a graph is connected if and only it has at most one connected component. Note that the empty graph (with the empty vertex-set) is connected and has zero connected components. And also note that every tree is a forest (but not the other way around).
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Forests (cont.)
Considering the so-called “disjoint union” of the two trees we have seen previously, we get a (proper) forest (now taking this as one graph — note the necessity of the renaming): 1 2 5 6 7 8
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
4
- 3
12
⑤ ⑤ ⑤ ⑤ ⑤ ⑤ ⑤ ⑤
11 10
PPPPPPPPPPPPPPP
9
PPPPPPPPPPPPPPP
Lemma 1 A graph G is a forest if and only if G contains no cycle.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Spanning forests
A graph G has a spanning tree if and only if G is not empty and G is connected. On the other hand, every graph has a spanning forest. These are precisly given by the spanning trees of the connected components (taken together). If we have a disconnected graph, then we can just break it up into its components, and treat them separately. In this sense the case of connected graphs and spanning trees is the central case. However for real software systems, we cannot just assume the graphs to be connected, and there we handle forests. This is just done by restarting BFS (or later DFS)
- n the vertices not covered yet.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
Breadth-first search
Searching through a graph is one of the most fundamental of all algorithmic tasks. Breadth-first search (BFS) is a simple but very important technique for searching a connected graph. Such a search starts from a given source vertex s and constructs a rooted spanning tree for the graph, called the breadth-first tree (the root is s). It uses a (first-in first-out) queue as its main data structure. BFS computes the parent π[u] of each vertex u in the breadth-first tree (with the parent of the source being nil), as well as its distance d[u] from the source s (initialised to ∞), which is the length of the path from s to u in the breadth-first tree, which is a shortest path between these vertices.
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
The algorithm
Input: A graph G with vertex set V (G) and edges represented by adjacency lists Adj. BFS(G, s) 1 for each u ∈ V (G) 2 d[u] = ∞ 3 π[s] = nil 4 d[s] = 0 5 Q = (s) 6 while Q = () 7 u = Dequeue[Q] 8 for each v ∈ Adj[u] 9 if d[v] = ∞ 10 d[v] = d[u] + 1 11 π[v] = u 12 Enqueue(Q, v)
CS 270 Algorithms Oliver Kullmann Graphs and directed graphs Representing graphs Trees Breadth-first search
BFS illustrated
Q = (1) 1 2 4 6 3 5 7
0/nil ∞/− ∞/− ∞/− ∞/− ∞/− ∞/−
u
d[u]/π[u]
(labelling) Q = (2) 1 2 4 6 3 5 7
0/nil 1/1 ∞/− ∞/− ∞/− ∞/− ∞/−
Q = (3, 4) 1 2 4 6 3 5 7
0/nil 1/1 2/2 ∞/− 2/2 ∞/− ∞/−
Q = (4, 5) 1 2 4 6 3 5 7
0/nil 1/1 2/2 ∞/− 2/2 3/3 ∞/−
Q = (5, 6, 7) 1 2 4 6 3 5 7
0/nil 1/1 2/2 3/4 2/2 3/3 3/4
Q = (6, 7) 1 2 4 6 3 5 7
0/nil 1/1 2/2 3/4 2/2 3/3 3/4
Q = (7) 1 2 4 6 3 5 7
0/nil 1/1 2/2 3/4 2/2 3/3 3/4
Q = () 1 2 4 6 3 5 7
0/nil 1/1 2/2 3/4 2/2 3/3 3/4