250 likes | 429 Views
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.
E N D
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 (VV) • Example: www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
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
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
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 53 = 15 which is an odd number. Not possible. • The sum is 54 = 20 = 2 |E| and 20/2 = 10 edges. May be possible. www.cs.sfu.ca/~jcliu/MACM101/09-Graph.ppt
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
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
Adjacency Lists Consists of an array Adj of |V| lists. One list per vertex. For uV, 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
Storage Requirement For directed graphs: Sum of lengths of all adj. lists is out-degree(v) = |E| vV Total storage:(|V|+|E|) For undirected graphs: Sum of lengths of all adj. lists is degree(v) = 2|E| vV 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.