week 4
play

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 Trees Breadth-first search Graphs and directed graphs Depth-first 1 search Topological sorting Trees 2 Breadth-first search 3


  1. CS 270 Algorithms Oliver Week 4 Kullmann Graphs and directed graphs Elementary Graph Algorithms Trees Breadth-first search Graphs and directed graphs Depth-first 1 search Topological sorting Trees 2 Breadth-first search 3 Depth-first search 4 Topological sorting 5

  2. CS 270 General remarks Algorithms Oliver Kullmann We consider graphs , an important data structure. Graphs and directed The two main algorithms are breadth-first search (BFS) graphs and depth-first search (DFS). Trees Breadth-first We consider one application of BFS, the computation of search shortest paths . Depth-first search And we consider one application of DFS, topological Topological sorting sorting of graphs. Reading from CLRS for week 4 Chapter 22 Plus appendices B.4 “Graphs” B.5.1 “Free trees”

  3. CS 270 Towards the mathematical notion of “graph” Algorithms Oliver Kullmann Definition A graph G is a pair G = ( V , E ) , where V is the set of vertices , while E is the set of (undirected) edges . An Graphs and directed (undirected) edge connecting vertices u , v is denoted by { u , v } . graphs We require u � = v here. Trees Breadth-first An example is search Depth-first search G = ( { 1 , 2 , 3 , 4 , 5 } , { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } , { 3 , 4 } } ) Topological sorting 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”.

  4. � � � CS 270 Directed graphs Algorithms A “graph” is synonymous with “undirected graph”. Now for a Oliver Kullmann “directed graph” the edges are directed: Graphs and directed Definition A directed graph (or digraph ) G is a pair graphs G = ( V , E ) , where V is again the set of vertices, while E is the Trees set of directed edges or arcs . A directed edge from vertex u Breadth-first search to vertex v is denoted by ( u , v ) . Again we require u � = v here. Depth-first search An example is Topological sorting 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: � 2 1 3 4 5

  5. CS 270 Remarks on these fundamental notions Algorithms A loop is an edge or arc connecting a vertex with itself: Oliver Kullmann 1 The notion of graph doesn’t allow loops; only the extension Graphs and directed of “graphs with loops” allows them, but we do not consider graphs them here. Trees 2 Also the notion of directed graph doesn’t allow loops Breadth-first search (different from the book we keep the symmetry between Depth-first “graphs” and “directed graphs”); the extension of “directed search Topological graphs with loops” allows them, but again we do not sorting consider them. For a graph G with n = | V | vertices we have � n � = n ( n − 1) | E | ≤ 2 2 while for a directed graph G we have | E | ≤ n ( n − 1) .

  6. CS 270 Representations of graphs Algorithms Oliver Kullmann Graphs and directed The above notions of graphs and digraphs yield mathematical graphs objects. These are the eternal platonic ideas, which have many Trees different representations in computers. Breadth-first search There are two main ways to represent a graph G = ( V , E ): Depth-first search Adjacency lists: The graph is represented by a V -indexed array Topological sorting of 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 A i , j = 1 if and only if vertex i is adjacent to vertex j .

  7. CS 270 Adjacency list representation Algorithms Oliver 1 2 4 6 Kullmann 1 2 Graphs and 2 1 3 4 directed 3 5 7 graphs 3 2 4 5 Trees 4 2 3 5 6 7 Breadth-first 5 3 4 7 search 6 4 Depth-first search 7 4 5 Topological sorting 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 .

  8. CS 270 Adjacency matrix representation Algorithms Oliver Kullmann Graphs and 1 2 4 6 directed 0 1 0 0 0 0 0   graphs 1 0 1 1 0 0 0 Trees 3 5 7     Breadth-first 0 1 0 1 1 0 0   search   A = 0 1 1 0 1 1 1   Depth-first   search 0 0 1 1 0 0 1     Topological 0 0 0 1 0 0 0   sorting 0 0 0 1 1 0 0 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 order of V 2 .

  9. CS 270 Comparing the two representations Algorithms Oliver Kullmann Graphs and Apart from the different space requirements needed for sparse directed graphs and dense graphs, there are other criteria for deciding on which Trees representation to use. Breadth-first search The choice of representation will depend mostly on what Depth-first questions are being asked. search Topological For example, consider the time required to decide the following sorting 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?

  10. CS 270 The notion of a tree Algorithms Oliver You might have seen already “rooted trees” (and you will in the Kullmann next week) — trees are basically just like rooted trees, but Graphs and directed without a designated root. graphs Trees Definition A tree is a graph T with at least one vertex, which is Breadth-first connected and does not have a cycle. search Depth-first Intuitively, a graph G has a cycle if there are two different search vertices u , v in G such that there are (at least) two essentially Topological sorting 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.

  11. CS 270 Spanning trees Algorithms Oliver Kullmann Graphs and directed Definition Consider a connected graph G with at least one graphs vertex. A spanning tree of G is a tree T with V ( T ) = V ( G ) Trees and E ( T ) ⊆ E ( G ) . Breadth-first search So spanning trees just leave out edges which are not needed to Depth-first search connect the whole graph. For example consider the graphs Topological sorting G 1 = 1 2 , G 2 = 1 2 . ❃ ❃ ❃ ❃ ❃ ❃ ❃ ❃ 3 4 3 4 G 1 has 4 different spanning trees, while G 2 has 4 + 4 = 8.

  12. CS 270 Computing spanning trees Algorithms Oliver We will see two algorithms, BFS and DFS, for computing Kullmann spanning trees: Graphs and directed graphs In both cases actually rooted spanning trees are Trees computed, that is, additionally a vertex is marked as “root”. Breadth-first search When drawing such rooted spanning trees, one starts with Depth-first the root (otherwise one could start with an arbitrary search vertex!), going from the root to the leaves . Topological sorting 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.

  13. CS 270 Representing rooted trees Algorithms Oliver Kullmann A rooted tree, that is, a tree together with a root T , will be Graphs and represented by BFS and DFS as follows: directed graphs Now there is a “direction” in the tree, either going from the Trees root towards the leaves, or from the leaves towards the root. Breadth-first search We obtain the usual notion of the children of a node Depth-first search (without a root, in a “pure tree”, there is no such thing). Topological The leaves are the nodes without children. sorting 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”).

  14. CS 270 Breadth-first search Algorithms Oliver Kullmann Searching through a graph is one of the most fundamental of all Graphs and algorithmic tasks. directed graphs Breadth-first search (BFS) is a simple but very important Trees technique for searching a connected graph. Breadth-first search Such a search starts from a given source vertex s and constructs Depth-first search a rooted spanning tree for the graph, called the breadth-first Topological tree (the root is s ). sorting 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.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend