CS 473Lecture 131 CS473-Algorithms I Lecture 13-A Graphs.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

CS 336 March 19, 2012 Tandy Warnow.
Great Theoretical Ideas in Computer Science
Great Theoretical Ideas in Computer Science for Some.
Based on slides by Y. Peng University of Maryland
Analysis of Algorithms CS 477/677
1 Decomposing Hypergraphs with Hypertrees Raphael Yuster University of Haifa - Oranim.
Chapter 9 Graphs.
Lecture 5 Graph Theory. Graphs Graphs are the most useful model with computer science such as logical design, formal languages, communication network,
Walks, Paths and Circuits Walks, Paths and Circuits Sanjay Jain, Lecturer, School of Computing.
Graph-02.
Bayesian Networks, Winter Yoav Haimovitch & Ariel Raviv 1.
 期中测验时间:本周五上午 9 : 40  教师 TA 答疑时间 : 周三晚上 6 : 00—8 : 30  地点:软件楼 315 房间,  教师 TA :李弋老师  开卷考试.
Review Binary Search Trees Operations on Binary Search Tree
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
GOLOMB RULERS AND GRACEFUL GRAPHS
Tirgul 8 Graph algorithms: Strongly connected components.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Applied Discrete Mathematics Week 12: Trees
SE561 Math Foundations Week 11 Graphs I
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Applied Discrete Mathematics Week 12: Trees
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Important Problem Types and Fundamental Data Structures
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
GRAPH Learning Outcomes Students should be able to:
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Based on slides by Y. Peng University of Maryland
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Data Structures & Algorithms Graphs
Chapter 1 Fundamental Concepts Introduction to Graph Theory Douglas B. West July 11, 2002.
1/24 Introduction to Graphs. 2/24 Graph Definition Graph : consists of vertices and edges. Each edge must start and end at a vertex. Graph G = (V, E)
September1999 CMSC 203 / 0201 Fall 2002 Week #13 – 18/20/22 November 2002 Prof. Marie desJardins.
Connectivity and Paths 報告人:林清池. Connectivity A separating set of a graph G is a set such that G-S has more than one component. The connectivity of G,
Graph Theory and Applications
Introduction to Graph Theory
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
 Quotient graph  Definition 13: Suppose G(V,E) is a graph and R is a equivalence relation on the set V. We construct the quotient graph G R in the follow.
8.4 Closures of Relations Definition: The closure of a relation R with respect to property P is the relation obtained by adding the minimum number of.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 9: Graphs.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Great Theoretical Ideas in Computer Science for Some.
COMPSCI 102 Introduction to Discrete Mathematics.
Graph Theory Graph Theory - History Leonhard Euler's paper on “Seven Bridges of Königsberg”, published in 1736.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Leda Demos By: Kelley Louie Credits: definitions from Algorithms Lectures and Discrete Mathematics with Algorithms by Albertson and Hutchinson graphics.
رياضيات متقطعة لعلوم الحاسب MATH 226. Chapter 10.
Trees.
Applied Discrete Mathematics Week 13: Graphs
Special Graphs By: Sandeep Tuli Astt. Prof. CSE.
Graph theory Definitions Trees, cycles, directed graphs.
Graph Algorithms Using Depth First Search
Basic Graph Algorithms
Graphs G = (V, E) V are the vertices; E are the edges.
Discrete Mathematics for Computer Science
Applied Discrete Mathematics Week 13: Graphs
Presentation transcript:

CS 473Lecture 131 CS473-Algorithms I Lecture 13-A Graphs

CS 473Lecture 132 Graphs A directed graph (or digraph) G is a pair (V, E), where V is a finite set, and E is a binary relation on V The set V: Vertex set of G The set E: Edge set of G Note that, self-loops -edges from a vertex to itself- are possible In an undirected graph G  (V, E) the edge set E consists of unordered pairs of vertices rather than ordered pairs, that is, (u, v) & (v, u) denote the same edge self-loops are forbidden, so every edge consists of two distinct vertices

CS 473Lecture 133 Graphs Many definitions for directed and undirected graphs are the same although certain terms have slightly different meanings If (u, v)  E in a directed graph G  (V, E), we say that (u, v) is incident from or leaves vertex u and is incident to or enters vertex v If (u, v)  E in an undirected graph G  (V, E), we say that (u, v) is incident on vertices u and v If (u, v) is an edge in a graph G  (V, E), we say that vertex v is adjacent to vertex u When the graph is undirected, the adjacency relation is symmetric When the graph is directed the adjacency relation is not necessarily symmetric if v is adjacent to u, we sometimes write u  v

CS 473Lecture 134 Graphs The degree of a vertex in an undirected graph is the number of edges incident on it In a directed graph, out-degree of a vertex: number of edges leaving it in-degree of a vertex : number of edges entering it degree of a vertex : its in-degree  its out-degree A path of length k from a vertex u to a vertex u in a graph G  (V, E) is a sequence  v 0, v 1, v 2, …, v k  of vertices such that v 0  u, v k  u and (v i  1, v i )  E, for i  1, 2, …, k The length of a path is the number of edges in the path

CS 473Lecture 135 Graphs If there is a path p from u to u, we say that u is reachable from u via p: u  u A path is simple if all vertices in the path are distinct A subpath of path p   v 0, v 1, v 2, …, v k  is a contiguous subsequence of its vertices That is, for any 0  i  j  k, the subsequence of vertices  v i, v i  1, …, v j  is a subpath of p In a directed graph, a path  v 0, v 1, …, v k  forms a cycle if v 0  v k and the path contains at least one edge The cycle is simple if, in addition, v 0, v 1, …, v k are distinct A self-loop is a cycle of length 1 p

CS 473Lecture 136 Graphs Two paths  v 0,v 1,v 2,…,v k  &  v 0,v 1,v 2,…,v k  form the same cycle if there is an integer j such that v i  v (i  j) mod k for i  0, 1,…, k  1 The path p 1   1, 2, 4, 1  forms the same cycles as the paths p 2   2, 4, 1, 2  and p 3   4, 1, 2, 4  A directed graph with no self-loops is simple In an undirected graph a path  v 0,v 1,…,v k  forms a cycle if v 0  v k and v 1,v 2,…,v k are distinct A graph with no cycles is acyclic

CS 473Lecture 137 Graphs An undirected graph is connected if every pair of vertices is connected by a path The connected components of a graph are the equivalence classes of vertices under the “is reachable from” relation An undirected graph is connected if it has exactly one component, i.e., if every vertex is reachable from every other vertex A directed graph is strongly-connected if every two vertices are reachable from each other The strongly-connected components of a digraph are the equivalence classes of vertices under the “are mutually reachable” relation A directed graph is strongly-connected if it has only one strongly-connected component

CS 473Lecture 138 Graphs Two graphs G  (V, E) and G  (V, E ) are isomorphic if there exists a bijection f : V  V such that (u, v)  E iff (f (u), f (v))  E That is, we can relabel the vertices of G to be vertices of G maintaining the corresponding edges in G and G G  (V, E) G  (V, E ) V  {1,2,3,4,5,6} V  {u,v,w,x,y,z} Map from V  V : f (1)  u, f (2)  v, f (3)  w, f (4)  x, f (5)  y, f (6)  z

CS 473Lecture 139 Graphs A graph G  (V, E ) is a subgraph of G  (V, E) if V  V and E  E Given a set V  V, the subgraph of G induced by V is the graph G  (V, E ) where E  {(u,v)  E: u,v  V } G  (V, E) G  (V, E ), the subgraph of G induced by the vertex set V  {1,2,3,6}

CS 473Lecture 1310 Graphs Given an undirected graph G  (V, E), the directed version of G is the directed graph G  (V, E ), where (u,v)  E and (v,u)  E  (u,v)  E That is, each undirected edge (u,v) in G is replaced in G by two directed edges (u,v) and (v,u) Given a directed graph G  (V, E), the undirected version of G is the undirected graph G  (V, E ), where (u,v)  E  u  v and (u,v)  E That is the undirected version contains the edges of G “with their directions removed” and with self-loops eliminated

CS 473Lecture 1311 Graphs Note: G G i.e., (u,v) and (v,u) in G are replaced in G by the same edge (u,v) In a directed graph G  (V, E), a neighbor of a vertex u is any vertex that is adjacent to u in the undirected version of G That, is v is a neighbor of u iff either (u,v)  E or (v,u)  E v is a neighbor of u in both cases In an undirected graph, u and v are neighbors if they are adjacent

CS 473Lecture 1312 Graphs Several kinds of graphs are given special names Complete graph: undirected graph in which every pair of vertices is adjacent Bipartite graph: undirected graph G  (V, E) in which V can be partitioned into two disjoint sets V 1 and V 2 such that (u,v)  E implies either u  V 1 and v  V 2 or u  V 2 and v  V 1

CS 473Lecture 1313 Graphs Forest: acyclic, undirected graph Tree: connected, acyclic, undirected graph Dag: directed acyclic graph Multigraph: undirected graph with multiple edges between vertices and self-loops Hypergraph: like an undirected graph, but each hyperedge, rather than connecting two vertices, connects an arbitrary subset of vertices h 1  (v 1, v 2 ) h 2  (v 2, v 5, v 6 ) h 3  (v 2, v 3, v 4, v 5 )

CS 473Lecture 1314 Free Trees A free tree is a connected, acyclic, undirected graph We often omit the adjective “free” when we say that a graph is a tree If an undirected graph is acyclic but possibly disconnected it is a forest

CS 473Lecture 1315 T heorem (Properties of Free Trees) The following are equivalent for an undirected graph G  (V,E) 1.G is a free tree 2.Any two vertices in G are connected by a unique simple-path 3.G is connected, but if any edge is removed from E the resulting graph is disconnected 4.G is connected, and |E|  |V|  1 5.G is acyclic, and |E|  |V|  1 6.G is acyclic, but if any edge is added to E, the resulting graph contains a cycle

CS 473Lecture 1316 Properties of Free Trees (1  2) (1) G is a free tree (2) Any two vertices in G are connected by a unique simple-path

CS 473Lecture 1317 Properties of Free Trees (1  2) Since a tree is connected, any two vertices in G are connected by a simple path Let two vertices u,v  V are connected by two simple paths p 1 and p 2 Let w and z be the first vertices at which p 1 and p 2 diverge and re-converge Let p 1 be the subpath of p 1 from w to z Let p 2 be the subpath of p 2 from w to z p 1 and p 2 share no vertices except their end points The path p 1 || p 2 is a cycle (contradiction)

CS 473Lecture 1318 Properties of Free Trees (1  2) p 1 and p 2 share no vertices except their end points p 1 || p 2 is a cycle (contradiction) Thus, if G is a tree, there can be at most one path between two vertices

CS 473Lecture 1319 Properties of Free Trees (2  3) (2) Any two vertices in G are connected by a unique simple-path (3) G is connected, but if any edge is removed from E the resulting graph is disconnected

CS 473Lecture 1320 Properties of Free Trees (2  3) If any two vertices in G are connected by a unique simple path, then G is connected Let (u,v) be any edge in E. This edge is a path from u to v. So it must be the unique path from u to v Thus, if we remove (u,v) from G, there is no path from u to v Hence, its removal disconnects G

CS 473Lecture 1321 Properties of Free Trees (3  4) Before proving 3  4 consider the following Lemma: any connected, undirected graph G  (V,E) satisfies |E|  |V|  1 Proof: Consider a graph G with |V| vertices and no edges. Thus initially there are |C|  |V| connected components –Each isolated vertex is a connected component Consider an edge (u,v) and let C u and C v denote the connected-components of u and v

CS 473Lecture 1322 Properties of Free Trees (Lemma) If C u  C v then (u,v) connects C u and C v into a connected component C uv Otherwise (u,v) adds an extra edge to the connected component C u  C v Hence, each edge added to the graph reduces the number of connected components by at most 1 Thus, at least |V|  1 edges are required to reduce the number of components to 1 Q.E.D

CS 473Lecture 1323 Properties of Free Trees (3  4) (3) G is connected, but if any edge is removed from E the resulting graph is disconnected (4) G is connected, and |E|  |V|  1

CS 473Lecture 1324 Properties of Free Trees (3  4) By assuming (3), the graph G is connected We need to show both |E|  |V|  1and |E|  |V|  1 in order to show that |E|  |V|  1 |E|  |V|  1: valid due previous lemma |E|  |V|  1: (proof by induction) Basis: a connected graph with n  1 or n  2 vertices has n  1 edges IH: suppose that all graphs G  (V,E) satisfying (3) also satisfy |E|  |V|  1

CS 473Lecture 1325 Properties of Free Trees (3  4) Consider G  (V,E) that satisfies (3) with |V|  n  3 Removing an arbitrary edge (u,v) from G separates the graph into 2 connected graphs G u  (V u,E u ) and G v  (V v,E v ) such that V  V u  V v and E  E u  E v Hence, connected graphs G u and G v both satisfy (3) else G would not satisfy (3) Note that |V u | and |V v |  n since |V u |  |V v |  n Hence, |E u |  |V u |  1 and |E v |  |V v |  1 (by IH) Thus, |E|  |E u |  |E v |  1  (|V u |  1)  (|V v |  1)  1 |E|  |V|  1  Q.E.D

CS 473Lecture 1326 Properties of Free Trees (4  5) (4) G is connected, and |E|  |V|  1 (5) G is acyclic, and |E|  |V|  1

CS 473Lecture 1327 Properties of Free Trees (4  5) Suppose that G is connected, and |E|  |V|  1, we must show that G is acyclic Suppose G has a cycle containing k vertices v 1, v 2, , v k Let G k  (V k,E k ) be subgraph of G consisting of the cycle If k  |V|, there must be a vertex v k  1  V  V k that is adjacent to some vertex v i  V k, since G is connected Note: | V k |  |E k |  k

CS 473Lecture 1328 Properties of Free Trees (4  5) Define G k  1  (V k  1,E k  1 ) to be subgraph of G with V k  1  V k  v k  1 and E k  1  E k  (v k  1,v i ) If k  1  |V|, we can similarly define G k  2  (V k  2,E k  2 ) to be the subgraph of G with V k  2  V k  1  v k  2 and E k  2  E k  1  (v k  2,v j ) for some v j  V k  1 where | V k  2 |  |E k  2 | Note: | V k  1 |  |E k  1 |

CS 473Lecture 1329 Properties of Free Trees (4  5) We can continue defining G k  m with | V k  m |  |E k  m | until we obtain G n  (V n,E n ) where n  |V| and V n  |V| and | V n |  |E n |  |V| Since G n is a subgraph of G, we have E n  E  |E|  |E n |  |V| which contradicts the assumption |E|  |V|  1 Hence G is acyclic Q.E.D

CS 473Lecture 1330 Properties of Free Trees (5  6) (5) G is acyclic, and |E|  |V|  1 (6) G is acyclic, but if any edge is added to E, the resulting graph contains a cycle

CS 473Lecture 1331 Properties of Free Trees (5  6) Suppose that G is acyclic and |E|  |V|  1 Let k be the number of connected components of G G 1  (V 1,E 1 ), G 2  (V 2,E 2 ), , G k  (V k,E k ) such that  V i  V; i =1 k V i  V j   ; 1  i, j  k and i  j  E i  E; i =1 k E i  E j   ; 1  i, j  k and i  j Each connected component G i is a tree by definition

CS 473Lecture 1332 Properties of Free Trees (5  6) Since (1  5) each component G i is satisfies |E i |  |V i |  1 for i =1,2, , k Thus Therefore, we must have k =1  | E i |   | V i |   1 i =1 k k k | E |  | V |  k

CS 473Lecture 1333 Properties of Free Trees (5  6) That is (5)  G is connected  G is a tree Since (1  2) any two vertices in G are connected by a unique simple path Thus, adding any edge to G creates a cycle

CS 473Lecture 1334 Properties of Free Trees (6  1) (6) G is acyclic, but if any edge is added to E, the resulting graph contains a cycle (1) G is a free tree

CS 473Lecture 1335 Properties of Free Trees (6  1) Suppose that G is acyclic but if any edge is added to E a cycle is created We must show that G is connected due to the definition Let u and v be two arbitrary vertices in G If u and v are not already adjacent adding the edge (u,v) creates a cycle in which all edges but (u,v) belong to G

CS 473Lecture 1336 Properties of Free Trees (6  1) Thus there is a path from u to v, and since u and v are chosen arbitrarily G is connected

CS 473Lecture 1337 Representations of Graphs The standard two ways to represent a graph G  (V,E) –As a collection of adjacency-lists –As an adjacency-matrix Adjacency-list representation is usually preferred –Provides a compact way to represent sparse graphs Those graphs for which | E |  | V | 2

CS 473Lecture 1338 Representations of Graphs Adjacency-matrix representation may be preferred –for dense graphs for which |E| is close to |V| 2 –when we need to be able to tell quickly if there is an edge connecting two given vertices

CS 473Lecture 1339 Adjacency-List Representation An array Adj of |V| lists, one for each vertex u  V For each u  V the adjacency-list Adj[u] contains (pointers to) all vertices v such that (u,v)  E That is, Adj[u] consists of all vertices adjacent to u in G The vertices in each adjacency-list are stored in an arbitrary order

CS 473Lecture 1340 Adjacency-List Representation If G is a directed graph –The sum of the lengths of the adjacency lists  | E | If G is an undirected graph –The sum of the lengths of the adjacency lists  2| E | since an edge (u,v) appears in both Adj[u] and Adj[v]

CS 473Lecture 1341 Representations of Graphs Undirected Graphs

CS 473Lecture 1342 Representations of Graphs Directed Graphs

CS 473Lecture 1343 Adjacency List Representation (continued) Adjacency list representation has the desirable property it requires O(max(V, E))  O(V  E) memory for both undirected and directed graphs Adjacency lists can be adopted to represent weighted graphs each edge has an associated weight typically given by a weight function w: E  R The weight w(u, v) of an edge (u, v)  E is simply stored with vertex v in Adj[u] or with vertex u in Adj[v] or both

CS 473Lecture 1344 Adjacency List Representation (continued) A potential disadvantage of adjacency list representation there is no quicker way to determine if a given edge (u, v) is present in G than to search v in Adj[u] or u in Adj[v] This disadvantage can be remedied by an adjacency matrix representation at the cost of using asymptotically more memory

CS 473Lecture 1345 Adjacency Matrix Representation Assume that, the vertices of G  (V, E) are numbered as 1,2,…,|V| Adjacency matrix rep. consists of a |V|  |V| matrix A=(a ij )  Requires  (V 2 ) memory independent of the number of edges |E| We define the transpose of a matrix A  (a ij ) to be the matrix A T  (a ij ) T given by a ij T  a ji Since in an undirected graph, (u,v) and (v,u) represent the same edge A  A T for an undirected graph That is, adjacency matrix of an undirected graph is symmetric Hence, in some applications, only upper triangular part is stored

CS 473Lecture 1346 Adjacency Matrix Representation Adjacency matrix representation can also be used for weighted graphs Adjacency matrix may also be preferable for reasonably small graphs Moreover, if the graph is unweighted rather than using one word of memory for each matrix entry adjacency matrix representation uses one bit per entry