Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group www.deeds.informatik.tu-darmstadt.de Introduction to Computer Science 2 Introduction.

Similar presentations


Presentation on theme: "© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group www.deeds.informatik.tu-darmstadt.de Introduction to Computer Science 2 Introduction."— Presentation transcript:

1 © Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group www.deeds.informatik.tu-darmstadt.de Introduction to Computer Science 2 Introduction to Graphs (2) 1.Utility + Definitions  Prof. Neeraj Suri

2 © DEEDS 2008Graphs and their Properties2 Reachability: Transitive Closure  Does a path exist between two nodes?  Routing, security access, garbage collection, etc.  Definition transitive closure:  In the transitive closure G + = ( V, E + ) of the graph G = ( V,E ) there exists an edge between the vertices u and v iff there is a path between u and v in G G + = { (u,v) |  a path p = u,...,v in G and ||p||  1 }

3 © DEEDS 2008Graphs and their Properties3 Transitive Closure (Reachability Graph) v1v1 v2v2 v3v3 v4v4 v1v1 v2v2 v3v3 v4v4 Graph GTransitive closure G + of G Note: This is NOT a self loop but a reachability path

4 © DEEDS 2008Graphs and their Properties4 Application: SW Process Dependencies  A needs the result from B and C  B needs the result from D and E  C needs the result from B and D  D needs no results  E needs the result from A and C Question: does such a program work?

5 © DEEDS 2008Graphs and their Properties5 Application: Process Dependencies C B D A E Cycle: A waits for B B waits for E E waits for A!  A needs the result from B and C  B needs the result from D and E  C needs the result from B and D  D needs no input results  E needs the result from A and C

6 © DEEDS 2008Graphs and their Properties6 Cycles in Graphs  Does a directed graph contain cycles?  Problem for resource allocation: deadlock avoidance  Def: A directed graph G is acyclic if there are no cycles in G v1v1 v3v3 v4v4 v2v2 v5v5 v6v6 v7v7 Example: „DAG“ = Directed Acyclic Graph

7 © DEEDS 2008Graphs and their Properties7 Theorem for Acyclic Graphs Theorem:  Every acyclic graph G has at least one vertex with (in- deg/source) d - (w) = 0 and at least one vertex with (out- deg/sink) d + (v) = 0. Proof: ( A general Reducio ad Absurdum approach) 1.Consider p is the longest path in G (exists as G is non-empty) and v k is the ending vertex in p 2.Assume: d + (v k )  0. Then there is an edge (v k, w). If w is in p there is a cycle  If w is not in p, then p is not the longest path  3.The assumption does not hold  the opposite is true, i.e.: d + (v k ) = 0. (Proof for d - (v k ) = 0 is analogous)

8 © DEEDS 2008Graphs and their Properties8 Planarity  Def.: A graph is planar, if there exists some geometric representation of G which can be drawn on a plane such that no two edges intersect (cross each other)  planar graph non-planar graph?

9 © DEEDS 2008Graphs and their Properties9 Basis Behind Theorem for Planar Graphs  Def.: Homeomorphic graphs are created by merging two edges when the mutual vertex of degree 2 is removed, or through insertion of vertices of degree 2  Def.: The foll. graphs are called Kuratowski graphs: (strictly non-planar; removal of a single edge makes them planar!) K3,3 K5

10 © DEEDS 2008Graphs and their Properties10 Theorem for Planar Graphs  Theorem:  A graph is planar, when it does not contain any of the Kuratowski graphs or their homeomorphic forms Test for Planarity (Reduction): 1.Components are analyzed separately 2.Remove self loops 3.Remove parallel edges 4.Merge vertices with degree d(v) = 2 (homeomorphic Graphs) The result is either (a) a single edge, (b) a complete graph with 4 nodes, or (c) a non-separable graph with n  5 vertices and for each vertex holds d(v)  3. Cases (a) and (b) are Planar. Case (c) requires comparison to the Kuratowski’s Graphs (and their homeomorphic forms)

11 © DEEDS 2008Graphs and their Properties11 Example: Test of Planarity Merge vertices  Removal of parallel edges 

12 © DEEDS 2008Graphs and their Properties12 Dominating Sets  A set of vertices form a dominating set iff every vertex not in the set is adjacent to at least one vertex in the set A BC D EF G H * Dominating Sets: AEFD, BCGH, AFH, … Minimal dominating set iff no proper subset of it is a dominating set: AFH, DE, BH, CG, … * Dominating # is the smallest # of vertices in a minimal dominating set

13 © DEEDS 2008Graphs and their Properties13 Theory of Alternating Transmitters  Every graph contains at least 2 disjoint minimal dominating sets A BC D EF G H Minimal dominating set iff no proper subset of it is a dominating set: AFH, DE, BH, CG, … Think of “checkmate” situations on a chessboard

14 © DEEDS 2008Graphs and their Properties14 CS Usage: Data Matrixes/Patterns  Given a binary matrix, find the smallest set of rows which covers a 1 in each column A B C D E 1 1 0 1 1 0 covers 1 0 0 1 0 1 111 111 1 1111 111 111

15 © DEEDS 2008Graphs and their Properties15 Adjacency Matrix, Dominating Sets  Given a binary matrix, find the smallest set of rows which covers a 1 in each column A B C D E 1 1 0 1 1 0 covers 1 0 0 1 0 1 111 111 1 111 1 111 ABCDEF 1 1 AB C DE F F 1 1 1

16 © DEEDS 2008Graphs and their Properties16 Labeling of Graphs  There are many problems where it is of interest to label the vertices or edges of a graph (e.g., graph coloring)  Def.: S is a any non-empty value set. A vertex labeling of a graph G = (V,E) is a mapping: f : V  S  The set S is called “coloring set”  Def.: A vertex labeling f : V  S is called proper coloring of the graph G = (V,E) if for all v i,v j  V (v i  v j ) and (v i,v j )  E or (v j,v i )  E holds: f (v i )  f (v j )

17 © DEEDS 2008Graphs and their Properties17 Coloring Problems  Chromatic number = the smallest number n, for which a graph can be n-colored (i.e., no 2 same colors are adjacent)  Coloring problem: is a graph k-chromatic? Mighty difficult problem (NP-Complete) Consider S = {red, blue, green}. Question: is there a coloring for a graph G with the given set of colors? v1v1 v3v3 v2v2 v4v4 v5v5

18 © DEEDS 2008Graphs and their Properties18 Coloring Problem (2)  Note: The complete graph with n vertices has the chromatic number n  The 4-color-problem for planar graphs (Every planar graph or map is 4-colorable) is still unsolved; only five-coloring solved  4-colorable “proof” by Haken und Appel (1976)  “proof” through computer aided (exhaustive) analysis of graphs from 1936+, for which all other graphs can be reduced to  Four Colors Suffice: How the Map Problem Was Solved (Robin Wilson)  Check out Wikipedia (Graph Coloring; Four Color Theorem)  Coloring problems arise for instance for register assignments in compilers

19 © DEEDS 2008Graphs and their Properties19 Coloring  Any graph where all the vertices are of degree (d) or less can be (d+1) colored  Koenig’s Theorem: A graph can be 2 colored iff it has no circuits of odd length

20 © DEEDS 2008Graphs and their Properties20 Edge Labeling  Analog to vertex labeling: S is any value set. An edge labeling is a mapping g : V  V  S  For an edge e = (u,v) we call g(e) the weight of e  Weights often represents distances of flows (e.g., capacities), for instance current, water, bandwidth etc. v1v1 v2v2 v3v3 v4v4 10km 5km 7km 3km Labeled graph

21 © DEEDS 2008Graphs and their Properties21 Edge Labeling (2)  Classical problems for edge labeled graphs are path problems and flow problems:  Which is the shortest path between vertex u and v?  How large is the maximum flow between vertex u and v?  Special form of weights with S = { 0, 1 }: Based on a complete graph G = (V, VV) is G f = (V, E f ), where E f = { e  V  V | f(e) = 1 }

22 © DEEDS 2008Graphs and their Properties22 Application: Route Planning Traveling Salesman: Find a circuit (or path) of minimum length such that all capitals in 50 states are visited.

23 © DEEDS 2008Graphs and their Properties23 Summary  Graphs are very general and very useful data structures in Computer Science  Many standard problems can be transformed into graph problems and are often solvable with standard algorithms  Graph algorithms (also distributed ones) generally have high complexity  In many cases one can use simpler structures and (hopefully) achieve better efficiency  Example: Trees (our next theme after graphs)

24 © DEEDS 2008Graphs and their Properties How should we represent this graph? 24 v4v4 v1v1 v3v3 v5v5 v2v2 Depends on our purpose, right…

25 © DEEDS 2008Graphs and their Properties25 Representation of Graphs in Computers  Goals: (a) efficient internal representation of graphs in computers, (b) efficient operations on graphs  efficient memory utilization  efficient initialization  efficient support for graph based operations/algorithms  Two basic forms of representation : Sequentially in memory (Matrix, Tables) Linked memory (with dynamically linked lists) (Object oriented versions)

26 © DEEDS 2008Graphs and their Properties26 Adjacency-Matrix Representation  Used for representing directed & undirected graphs  For G = ( V,E ) with V = { v 1, v 2,..., v m }. The adjacency-matrix of G is an m  m binary matrix A, such that: A i,k = 1if (v i,v k )  E [Row i, Element j] A i,k = 0 otherwise  Symmetry in adjacency-matrices of undirected graphs  Depending on the order in which the nodes are processed, the adjacency-matrix representation can differ (the rows, or the columns can be interchanged)

27 © DEEDS 2008Graphs and their Properties27 Example: Adjacency-Matrix (Node Basis)  Edges represented by 1  entry in row (from) and column (to)  (If needed) Multiple edges (or even paths) can be represented with a non-binary matrix v4v4 v1v1 v3v3 v5v5 v2v2 00001 10010 10000 00000 00010 A = to from 1 15

28 © DEEDS 2008Graphs and their Properties28 Assessment: Adjacency-Matrix  Representation is ideal, if the purpose is to access certain edges often, since this the complexity will be always O(1)..  The adjacency-matrix of a graph needs |V| 2 memory locations.  The graph algorithms, that need sequential access to all the edges, usually need O(|V| 2 ) time.  In case of low number of edges, the adjacency-matrix is sparse. In these cases, it is preferable to use alternate representation with O(|E|) memory requirements. p. 528

29 © DEEDS 2008Graphs and their Properties29 Incidence Matrix: Edge Basis  The incidence matrix stores nodes (rows) and edges (columns) (or vice versa)  Useful primarily for undirected graphs |V|.|E|  Nice if |E| is small… for fully connected graph, |E|…??? v1v1 v3v3 v4v4 v5v5 v2v2 11010 00011 10000 00101 01100 Nodes a b c d e Edges a b c d e v1v2v3v4v5v1v2v3v4v5 p. 528

30 © DEEDS 2008Graphs and their Properties30 Table Representation: Edge Basis  Based on storing the edges (undirected or directed)  Directed graphs using the order source-destination (from-to)  Edge identifier (id) optional v1v1 v3v3 v4v4 v5v5 v2v2 a b c d e (id) from to a31 b15 c54 d21 e24

31 © DEEDS 2008Graphs and their Properties31 Adjacency Tables: Using Vertices Out Links v0v0 v2v2 v3v3 v4v4 v1v1 0:  4 1:  0  3 2:  0 3: 4:  1  2  3 Length of links can make the table size grow unwieldy Can we bound the table ( for # of columns)?

32 © DEEDS 2008Graphs and their Properties32 Example: Bounded Adjacency Table 0:4 1:05 2:0 3: 4:16 5:3 6:27 7:3 Adjacency Table T from G Graph G V0V0 V3V3 V2V2 V1V1 V4V4 for v 1 for v 4 max. k = |V| + |E| entries

33 © DEEDS 2008Graphs and their Properties33 Adjacency Table  Adjacency tables have max. k = |V| + |E| entries, Dimension: T[k][2]  T[i][0] gives a node index, where the nodes are numbered from 0 to |V| - 1.  T[i][1] gives a table index from T.  All the edges that start from the nodes v j can be determined from T[ j ]: (v j, v T[ j ][0] )  E if T[ j ][ 0 ]  -1 Given  i, i=1, 2,..., m defined as  1 = T[ j ][1]  i = T[ i-1 ][1] for i = 2,...,m m is the minimum index with  m = -1. So (v j,v T[ i ][0] )  E for i = 1, 2,..., m - 1. 0:4 1:05 2:0 3: 4:16 5:3 6:27 7:3

34 © DEEDS 2008Graphs and their Properties34 Example: Adjacency Table  Matrix or table not flexible enough in general  Difficult to manage if all edges have costs associated, or coping with dynamic changes 0:4 1:05 2:0 3: 4:16 5:3 6:27 7:3 Adjacency Table T from G Graph G V0V0 V3V3 V2V2 V1V1 V4V4 for v 1 for v 4 max. k = |V| + |E| entries


Download ppt "© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group www.deeds.informatik.tu-darmstadt.de Introduction to Computer Science 2 Introduction."

Similar presentations


Ads by Google