1 / 24

Introduction to Graphs

Introduction to Graphs. Graph Definition. Graph : consists of vertices and edges. Each edge must start and end at a vertex. Graph G = ( V , E ) V = set of vertices E = set of edges  ( V  V ) Example:. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt. Graph Definition.

jalen
Download Presentation

Introduction to Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Graphs

  2. Graph Definition • Graph : consists of vertices and edges. Each edge must start and end at a vertex. • Graph G = (V, E) • V = set of vertices • E = set of edges  (VV) • Example: www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  3. Graph Definition • Degree of a vertex : number of edges connected to it • Example: V4 has degree = 3 V1 V2 V3 V4 V5 www.cod.edu/people/faculty/hubbardd/presentations/Math%201218/MTH1218%20Chapter%204.ppt

  4. Graph Definition • Total degree of a graph : sum of the degrees of all the vertices. Note: If a graph has n edges, the total degree = 2n. • Example: graph has total degree = 10 V2 V1 V4 V3 www.cod.edu/people/faculty/hubbardd/presentations/Math%201218/MTH1218%20Chapter%204.ppt

  5. Graph Definition • The Handshaking Theorem: • Let G = (V, E). Then • Proof: • Each edge contributes twice to the degree count of all vertices. • Example: • If a graph has 5 vertices, can each vertex have degree 3? Or 4? • The sum is 53 = 15 which is an odd number. Not possible. • The sum is 54 = 20 = 2 |E| and 20/2 = 10 edges. May be possible. www.cs.sfu.ca/~jcliu/MACM101/09-Graph.ppt

  6. Graph Definition • Types of graphs • Undirected: edge (u, v) = (v, u); for all v, • (v, v)  E (usually no self loops) • Some may have self loops. • Directed: (u, v) is edge from u to v, denoted as u  v. • Self loops are allowed. • Is a graph where an edge represents a one-way relation only. • Cf. undirected graph – an edge represents two-way or symmetric relationship between two vertices. • The number of directed edges which initiate from vertex v is called the outdegree of v or outdeg(v). • The number of directed edges which terminate at vertex v is called the indegree of v or indeg(v). www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  7. Representation of Graphs Two standard ways Adjacency Lists. Adjacency Matrix. a b b d c a 1 2 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 a b a c b c d a b c d c d d a c 3 4 www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  8. Adjacency Lists Consists of an array Adj of |V| lists. One list per vertex. For uV, Adj[u] consists of all vertices adjacent to u. a b b d c a a c b c d a b c d d a c a b b d c a c b If weighted, also store weights in adjacency lists. c d c d d www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  9. Storage Requirement For directed graphs: Sum of lengths of all adj. lists is out-degree(v) = |E| vV Total storage:(|V|+|E|) For undirected graphs: Sum of lengths of all adj. lists is degree(v) = 2|E| vV Total storage:(|V|+|E|) No. of edges leaving v No. of edges incident on v. Edge (u,v) is incident on vertices u and v. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  10. Pros and Cons: adj list Pros Space-efficient, when a graph is sparse. Can be modified to support many graph variants. Cons Determining if an edge (u,v) G is not efficient. Have to search in u’s adjacency list. (degree(u)) time. (V) in the worst case. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  11. Adjacency Matrix |V|  |V| matrix A. Number vertices from 1 to |V| in some arbitrary manner. A is then given by: 1 2 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 a b c d 3 4 1 2 1 2 3 4 1 0 1 1 1 2 0 0 1 0 3 0 0 0 1 4 0 0 0 0 If weighted, store weights also in adjacency matrix. a b c d 4 3 A = AT for undirected graphs. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  12. Space and Time Space:(|V|2). Not memory efficient for large graphs. Time:to list all vertices adjacent to u: (|V|). Time:to determine if (u, v)E: (1). Can store weights instead of bits for weighted graph. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  13. Directed Graphs • Theorem • Examples (continued): • Adjacency matrix of a directed graph: • outdeg(V1) = 1, indeg(V1) = 2 • outdeg(V2) = 2, indeg(V2) = 1 • outdeg(V3) = 0, indeg(V3) = 2 • outdeg(V4) = 2, indeg(V4) = 0 www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

  14. Weighted Graphs • Each edge e has a weight, wt(e) • graph may be undirected or directed • weight may represent length, cost, capacity, etc • adjacency matrix becomes weight matrix • adjacency lists include weight in node 5 4 v w 5 7 6 4 5 y x 8 6 7 z a weighted graph G www.dcs.gla.ac.uk/~rwi/alg3/Lecture6.ppt

  15. A set of vertices that cover all edges, i.e., at least one vertex of all edges is in the set. I.e., set of vertices W Í V with for all {x,y} Î E: xÎ W or y Î W. Vertex Cover problem: Given G, find vertex cover of minimum size Vertex cover Modified from www.cs.uu.nl/docs/vakken/na/na10-2005b.ppt

  16. Independent Set (I) Subset S of vertices such that no two vertices in S are connected www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt

  17. Independent Set (II) Subset S of vertices such that no two vertices in S are connected www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt

  18. Independent Set (III) OPTIMIZATION VERSION: • INSTANCE: graph G • SOLUTION: independent set S in G • MEASURE: maximize the size of S DECISION VERSION: • INSTANCE: graph G, number K • QUESTION: does G have independent set of size  K www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt

  19. Clique (I) Subset S of vertices such that every two vertices in S are connected www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt

  20. Clique Clique (II) INSTANCE: graph G, number K QUESTION: does G have a clique of size  K? www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt

  21. The following are equivalent G has an independent set with at least k vertices The complement of G has a clique with at least k vertices G has a vertex cover with at most n-k vertices Relations www.cs.uu.nl/docs/vakken/na/na10-2005b.ppt

  22. Bipartite Graphs • A bipartite graphG is • a graph in which the vertices V can be partitioned into two disjoint subsetsV1and V2such that • no two vertices in V1 are adjacent; • no two vertices in V2 are adjacent. • A complete bipartite graphKm,nis • Is a bipartite graph in which the sets V1 and V2 contain m and n vertices, respectively, and every vertex in V1 is adjacent to every vertex in V2. • Q: How many edges ? http://www.cs.sfu.ca/~jcliu/MACM101/09-Graph.ppt

  23. Assignment Problem • Assignment problem. • Input: weighted, complete bipartite graph G = (L  R, E)with |L| = |R|. • Goal: find a perfect matching of min weight. 1' 2' 3' 4' 5' 3 8 9 15 10 1 Min cost perfect matching M = { 1-2', 2-3', 3-5', 4-1', 5-4' } cost(M) = 8 + 7 + 10 + 8 + 11 = 44 4 10 7 16 14 2 9 13 11 19 10 3 8 13 12 20 13 4 1 7 5 11 9 5 An Introduction to Bioinformatics Algorithms www.bioalgorithms.info

  24. Assignment problem and related problems • L: n jobs, R: m machines, weight of an edge (i,j) in E: time for job i taken on machine j. • Assign n jobs to each of m machines such that the total processing time is minimized. • Matching: is a subset of E such that no two edges in the subset are adjacent. • Perfect matching: A matching saturates all vertices. • Maximum matching: A matching with the largest possible number of edges. • Min-cost maximum matching problem: Find a max matching in a graph (with edge weights) such that the total edge weight is minimized.

More Related