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 Representing graphs Trees Breadth-first search Graphs and directed graphs 1 Representing graphs 2 Trees 3 Breadth-first search 4 CS


  1. 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

  2. CS 270 General remarks Algorithms Oliver Kullmann Graphs and directed We consider graphs , an important data structure. graphs We consider the simplest graph-search algorithm, Representing graphs breadth-first search (BFS). Trees We consider one application of BFS, the computation of Breadth-first search 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”

  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. Representing graphs An example is Trees Breadth-first search 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”.

  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 Representing graphs set of directed edges or arcs . A directed edge from vertex u Trees to vertex v is denoted by ( u , v ) . Again we require u � = v here. Breadth-first search 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: � 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. Representing graphs 2 Also the notion of directed graph doesn’t allow loops Trees (different from the book we keep the symmetry between Breadth-first “graphs” and “directed graphs”); the extension of “directed search graphs with loops” allows them, but again we do not 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 Representing graphs different representations in computers. Trees There are two main ways to represent a graph G = ( V , E ): Breadth-first search Adjacency lists: The graph is represented by a V -indexed array 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 Representing 4 2 3 5 6 7 graphs 5 3 4 7 Trees 6 4 Breadth-first search 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 .

  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 Representing 3 5 7   graphs   0 1 0 1 1 0 0   Trees   A = 0 1 1 0 1 1 1   Breadth-first   search 0 0 1 1 0 0 1     0 0 0 1 0 0 0   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 Representing representation to use. graphs Trees The choice of representation will depend mostly on what Breadth-first questions are being asked. search 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?

  10. CS 270 The notion of a tree Algorithms Oliver You might have seen already “rooted trees” (and you will see Kullmann more of them in the following weeks) — trees are basically just Graphs and directed like rooted trees, but without a designated root. graphs Representing Definition A tree is a graph T with at least one vertex, which is graphs connected and does not have a cycle. Trees Breadth-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 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 Examples Algorithms Here are two trees: Oliver Kullmann 1 2 1 2 3 4 Graphs and ◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ◆◆◆◆◆◆◆◆◆◆◆◆◆◆ ♣♣♣♣♣♣♣♣♣♣♣♣♣♣ directed � � � � graphs � � � � � � � � Representing � � � � graphs 4 3 8 7 6 5 Trees And here is the smallest connected graph with at least one Breadth-first search 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

  12. 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 ) Representing graphs and E ( T ) ⊆ E ( G ) . Trees So spanning trees just leave out edges which are not needed to Breadth-first search connect the whole graph. For example consider the graphs 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.

  13. 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 Representing computed, that is, additionally a vertex is marked as “root”. graphs Trees When drawing such rooted spanning trees, one starts with Breadth-first the root (otherwise one could start with an arbitrary search 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.

  14. 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 Representing graphs root towards the leaves, or from the leaves towards the root. Trees We obtain the usual notion of the children of a node Breadth-first search (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”).

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