Graphs (Γράφοι) Graphs (Γράφοι) Graphs Graphs • Graphs are collections of nodes in which various pairs are connected by K08 Δομές Δεδομένων και Τεχνικές Προγραμματισμού line segments. The nodes are usually called vertices ( κορυφές ) and the Κώστας Χατζηκοκολάκης line segments edges (ακμές). • Graphs are more general than trees . Graphs are allowed to have cycles and can have more than one connected component. • Some authors use the terms nodes (κόμβοι) and arcs (τόξα) instead of vertices and edges. / / 1 2 Example of Graphs (Directed) Example of Graphs (Directed) Example of Graphs (Undirected) Example of Graphs (Undirected) / / 3 4
Examples of Graphs Examples of Graphs Examples Examples • Transportation networks • A network of oil pipelines • Interesting problem : What is the path with one or more stops of shortest • Interesting problem : What is the maximum possible overall �ow of oil overall distance connecting a starting city and a destination city? from the source to the destination? / / 5 6 Examples Examples Examples Examples • The Internet • The Web • Interesting problem : Deliver an e-mail from user A to user B • Interesting problem : What is the PageRank of a Web site? / / 7 8
Examples Examples Formal De�nitions Formal De�nitions G = ( V , E ) • The Facebook social network • A graph consists of a set of vertices V and a set of edges E , where the edges in are formed from pairs of distinct vertices in . E V • Interesting problem : Are John and Mary connected? What interesting clusters exist? • If the edges have directions then we have a directed graph (κατευθυνόμενο γράφο) or digraph . In this case edges are ordered pairs ( u , v ) ( u , v ) of vertices e.g., and are called directed . If is a directed edge then is called its origin and is called its destination . u v • If the edges do not have directions then we have an undirected graph (μη-κατευθυνόμενος γράφο) . In this case edges are unordered pairs of u , v vertices e.g., { } and are called undirected . • For simplicity, we will use the directed pair notation noting that in the ( u , v ) ( v , u ) undirected case is the same as . • When we say simply graph, we will mean an undirected graph. / / 9 10 Example of a Directed Graph Example of a Directed Graph Example of an Undirected Graph Example of an Undirected Graph 5 5 2 2 6 6 1 1 4 4 7 7 3 3 8 8 9 9 10 10 11 11 G = ( V , E ) G = ( V , E ) V = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 V = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 E = (1, 2), (1, 3), (2, 5), (3, 4), (5, 4), (5, 6), (6, 70, (8, 9), (8, 10), (10, 11) E = (1, 2), (1, 3), (2, 5), (3, 4), (5, 4), (5, 6), (6, 70, (8, 9), (8, 10), (10, 11) / / 11 12
More De�nitions More De�nitions De�nitions De�nitions v , v G = ( V , E ) • • A directed path is a path such that all edges are directed and are traversed Two di�erent vertices in a graph are said to be i j along their direction. ( v , v ) ∈ adjacent (γειτονικές) if there exists an edge . E i j • A directed cycle is similarly de�ned. • An edge is said to be incident (προσπίπτουσα) on a vertex if the vertex is one of the edge's endpoints. G = ( V , E ) • A path (μονοπάτι) in a graph , is a sequence of vertices of p p = v v … v , ( n ≥ 2) of the form in which each vertex , is V v i 1 2 n 1 ≤ i ≤ n − 1 adjacent to the next one (for ). v i +1 • The length of a path is the number of edges in it. • A path is simple if each vertex in the path is distinct. p = v v … v • A cycle is a path of length greater than one that begins 1 2 n v = and ends at the same vertex (i.e., ). v n 1 / / 13 14 De�nitions De�nitions Example Example • A simple cycle is a path that travels through three or more distinct 5 vertices and connects them into a loop. 4 6 8 7 2 1 3 Four simple cycles: (1,2,3,1) (4,5,6,7,4) (4,5,6,4) (4,6,7,4) / / 15 16
Example Example Example Example 5 5 4 6 4 6 8 8 7 7 2 2 1 3 1 3 Two non-simple cycles: (1,2,1) (4,5,6,4,7,6,4) A path that is not a cycle: (1,2,4,6,8) / / 17 18 Connectivity and Components Connectivity and Components Connectivity and Components Connectivity and Components • G = ( V , E ) • G = ( V , E ) Two vertices in a graph are said to be connected In the graph , a connected component (συνεκτική (συνδεδεμένες) if there is a path from the �rst to the second in G συνιστώσα) is a subset of the vertices S V that are all connected to one another. x ∈ V and y ∈ V , where x = y • Formally, if , then and are x y • A connected component of is a maximal connected component p = v v … v ∈ x = v 1 S G connected if there exists a path in such that G 1 2 n (μέγιστη συνεκτική συνιστώσα) provided there is no bigger subset of y = v n T and vertices in such that properly contains and such that itself is a V T S T connected component of . G • An undirected graph G can always be separated into maximal connected ∅ S , S , … ; , S S ∩ S = i = j components such that whenever . 1 2 n i j / / 19 20
Example of Undirected Graph and its Example of Undirected Graph and its Connectivity and Components in Directed Connectivity and Components in Directed Separation into Two Maximal Connected Separation into Two Maximal Connected Graphs Graphs Components Components • A subset of vertices in a directed graph is strongly connected S G ( v , v ) (ισχυρά συνεκτικό) if for each pair of distinct vertices in , is S v i i j connected to and is connected to . v j v j v i • A subset of vertices in a directed graph is weakly connected S G ( v , v ) (ασθενώς συνεκτικό) if for each pair of distinct vertices in , is S v i i j connected to or is connected to . v j v j v i / / 21 22 Example: A Strongly Connected Digraph Example: A Strongly Connected Digraph Example: A Weakly Connected Digraph Example: A Weakly Connected Digraph / / 23 24
Degree in Undirected Graphs Degree in Undirected Graphs Example Example • In an undirected graph G the degree ( βαθμός ) of vertex is the number x 5 of edges in which is one of the endpoints of . e x e 4 6 • deg( x ) The degree of a vertex is denoted by . x 8 7 2 1 3 . The degree of node 1 is 2. The degree of node 4 is 4. The degree of node 8 is 1. / / 25 26 Predecessors and Successors in Directed Graphs Predecessors and Successors in Directed Graphs In-Degree and Out-Degree in Directed Graphs In-Degree and Out-Degree in Directed Graphs • G = ( V , E ) • If is a vertex in a directed graph then the set of The in-degree of a vertex is the number of predecessors of x x x Pred( x ) predecessors (προηγούμενων) of denoted by is the set of all x • The out-degree of a vertex is the number of successors of x x y ∈ V ( y , x ) ∈ E vertices such that . • We can also de�ne the in-degree and the out-degree by referring to the Succ( x ) • Similarly the set of successors (επόμενων) of denoted by x is incoming and outgoing edges of a vertex. y ∈ V ( x , y ) ∈ E the set of all vertices such that . • indeg( x ) The in-degree and out-degree of a vertex are denoted by and x outdeg( x ) respectively. / / 27 28
Example Example Proposition Proposition • If G is an undirected graph with m edges, then 1 ∑ deg( v ) = 2 3 v ∈ G . 4 5 • Proof? - Each edge is counted twice The in-degree of node 4 is 2. The out-degree of node 4 is 1. / / 29 30 Proposition Proposition Proposition Proposition • If is a directed graph with edges, then • Let be a graph with vertices and edges. If is undirected, then G n m G n ( n −1) m ≤ m ≤ n ( n − 1) and if is directed, then . G ∑ ∑ 2 indeg( v ) = outdeg( v ) = m • Proof? v ∈ G v ∈ G n − 1 - If is undirected then the maximum degree of a vertex is . G • Proof? Therefore, from the previous proposition about the sum of the - Each edge is counted once 2 m ≤ n ( n − 1). degrees, we have n − 1 - If is directed then the maximum in-degree of a vertex is . G Therefore, from the previous proposition about the sum of the in- m ≤ n ( n − 1) degrees, we have . / / 31 32
Recommend
More recommend