Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 1/24 Introduction to Graphs

2 2/24 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 3/24 Graph Definition Degree of a vertex : number of edges connected to it Example: V 4 has degree = 3 V2V2 V5V5 V3V3 V4V4 V1V1 www.cod.edu/people/faculty/hubbardd/presentations/Math%201218/MTH1218%20Chapter%204.ppt

4 4/24 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 V2V2 V3V3 V4V4 V1V1 www.cod.edu/people/faculty/hubbardd/presentations/Math%201218/MTH1218%20Chapter%204.ppt

5 5/24 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 6/24 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 7/24 Representation of Graphs Two standard ways –Adjacency Lists. –Adjacency Matrix. a dc b a b c d b a d dc c ab ac a dc b 12 34 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

8 8/24 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 dc b a b c d b c d dc a dc b a b c d b a d dc c ab ac If weighted, also store weights in adjacency lists. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

9 9/24 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 10/24 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 11/24 Adjacency Matrix |V|  |V| matrix A. Number vertices from 1 to |V| in some arbitrary manner. A is then given by: a dc b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 0 0 1 0 3 0 0 0 1 4 0 0 0 0 a dc b 12 34 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 = A T for undirected graphs. www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt If weighted, store weights also in adjacency matrix.

12 12/24 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 13/24 Directed Graphs –Theorem –Examples (continued): Adjacency matrix of a directed graph: –outdeg(V 1 ) = 1, indeg(V 1 ) = 2 –outdeg(V 2 ) = 2, indeg(V 2 ) = 1 –outdeg(V 3 ) = 0, indeg(V 3 ) = 2 –outdeg(V 4 ) = 2, indeg(V 4 ) = 0 www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt

14 14/24 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 4 6 6 7 4 5 7 5 5 8 v w y x z a weighted graph G www.dcs.gla.ac.uk/~rwi/alg3/Lecture6.ppt Weighted Graphs

15 15/24 Vertex cover 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 Modified from www.cs.uu.nl/docs/vakken/na/na10-2005b.ppt

16 16/24 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 (I)

17 17/24 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)

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

19 19/24 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 (I)

20 20/24 Clique 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 Clique (II)

21 21/24 Relations 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 www.cs.uu.nl/docs/vakken/na/na10-2005b.ppt

22 22/24 Bipartite Graphs A bipartite graph G is –a graph in which the vertices V can be partitioned into two disjoint subsets V 1 and V 2 such that no two vertices in V 1 are adjacent; no two vertices in V 2 are adjacent. A complete bipartite graph K m,n is –Is a bipartite graph in which the sets V 1 and V 2 contain m and n vertices, respectively, and every vertex in V 1 is adjacent to every vertex in V 2. –Q: How many edges ? http://www.cs.sfu.ca/~jcliu/MACM101/09-Graph.ppt

23 23/24 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 1'2'3'4'5' Min cost perfect matching M = { 1-2', 2-3', 3-5', 4-1', 5-4' } cost(M) = 8 + 7 + 10 + 8 + 11 = 44 3891510 4 71614 913111910 813122013 175119 www.bioalgorithms.infoAn Introduction to Bioinformatics Algorithms

24 24/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.


Download ppt "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)"

Similar presentations


Ads by Google