1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R5. Graphs.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
Chapter 9: Graphs Basic Concepts
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Social Media Mining Graph Essentials.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
7.1 and 7.2: Spanning Trees. A network is a graph that is connected –The network must be a sub-graph of the original graph (its edges must come from the.
1 CS104 : Discrete Structures Chapter V Graph Theory.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Data Structures & Algorithms Graphs
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 10. Graphs.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Graph Theory and Applications
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graph theory and networks. Basic definitions  A graph consists of points called vertices (or nodes) and lines called edges (or arcs). Each edge joins.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
CSC2100B Tutorial 10 Graph Jianye Hao.
Graphs Upon completion you will be able to:
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 10. Graphs.
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.
CSCE 210 Data Structures and Algorithms
Graphs Lecture 19 CS2110 – Spring 2013.
Basic Concepts Graphs For more notes and topics visit:
Graph theory Definitions Trees, cycles, directed graphs.
Introduction to Graphs
CS120 Graphs.
Graph Algorithm.
Modeling and Simulation NETW 707
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Chapter 9: Graphs Basic Concepts
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graph Operations And Representation
Chapter 11 Graphs.
Text Book: Introduction to algorithms By C L R S
Chapter 9: Graphs Basic Concepts
Chapter 9 Graph algorithms
GRAPHS.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

1 Data Structures and Algorithms Graphs

2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms: Graph Traversal Shortest Paths Minimum Cost Spanning Trees

3 1. Basic Definitions A graph G (V,E) can be defined as a pair (V,E), where V is a set of vertices, and E is a set of edges between the vertices E = {(u,v) | u, v  V}. e.g. V = {A,B,C,D,E,F,G} E = {( A,B),(A,F),(B,C),(C,G),(D,E),(D,G),(E,F),(F,G)} If no weights are associated with the edges, an edge is either present(“1”) or absent (“0”). A F GBE DC

4 Basic Definitions Adjacency: If vertices u,v have an edge e = (u,v) | u, v  V then u and v are adjacent. A weighted graph has a weight associated with each edge. Undirected Graph is a graph in which the adjacency is symmetric, i.e., e = (u,v) = (v,u) A Sub-Graph: has a subset of the vertices and the edges A F GBE DC

5 Basic Definitions Directed Graph: is a graph in which adjacency is not symmetric, i.e., (u,v)  (v,u) Directed Weighted Graph: A directed graph with a weight for each edge. Also called a network. A F GBE DC

6 2. Paths & Cycles Path: A list of vertices of a graph where each vertex has an edge from it to the next vertex. Simple Path: A path that repeats no vertex. Cycle: A path that starts and ends at the same vertex and includes other vertices at most once. A F GBE DC AF GB E DC

7 Paths & Cycles Hamiltonian Cycle: A cycle that includes all other vertices only once, e.g. {D,B,C,G,A,F,E,D} Euler Circuit: A cycle that includes every edge once. Directed Acyclic Graph (DAG): A directed graph with no path that starts and ends at the same vertex AF GB E DC AF GB E DC

8 3. Connectivity Connected Graph: An undirected graph with a path from every vertex to every other vertex A Disconnected Graph may have several connected components Tree: A connected Acyclic graph A F GBE DC A F GBE DC

9 Connected Components Demo What happens when you start with an empty graph and add random edges between vertices? As you add more and more edges, the number of connected components in the graph can be expected to drop, until finally the graph is connected. An important result from the theory of random graphs states that such graphs very quickly develop a single ``giant'' component which eventually absorbs all the vertices.

10 Connectivity Articulation Vertex: if removed with all of its edges will cause a connected graph to be disconnected, e.g., G and D are articulation vertices A F BE DC A F GBE DC

11 Connectivity Articulation Vertex: if removed with all of its edges will cause a connected graph to be disconnected, e.g., G and D are articulation vertices A F BE DC A F GBE DC

12 Connectivity Degree Of a vertex, the number of edges connected to it. Degree Of a graph, the maximum degree of any vertex (e.g. B has degree 2, graph has degree 3). In a connected graph the sum of the degrees is twice the number of edges, i.e A F GBE DC

13 Connectivity In-Degree/Out-Degree: the number of edges coming into/emerging from a vertex in a connected graph (e.g. G has in-degree 3 and out-degree 1). AF GB E DC

14 Connectivity Complete Graph: There is an edge between every vertex and every other vertex. In this case, the number of edges is maximum: Notice that the minimum number of edges for a connected graph ( a tree in this case) is (V-1) A D CB

15 Connectivity Dense Graph: Number of edges is close to E max = V(V-1)/2. So, E =  (V 2 ) Sparse Graph: Number of edges is close to E min = (V-1). So, E = O(V)

16 4. Other Properties Planar Graph: A graph that can be drawn in the plain without edges crossing A D CB A D CB Non-Planar

17 Other Properties Graph Coloring: To assign color (or any distinctive mark) to vertices such that no two adjacent vertices have the same color. The minimum number of colors needed is called the Chromatic Order of the graph  (G). For a complete graph,  (G) = V

18 Other Properties Bipartite Graphs A bipartite graph is a graph whose vertices can be partitioned into two subsets X and Y such that each edge has one end in X and one in Y.

19 5. Representation Adjacency Matrix: V x V Matrix a(i,j) a(i,j) = 1 if vertices (i) and (j) are adjacent, zero otherwise. Usually self loops are not allowed so that a(i,i) = 0. For undirected graphs, a(i,j) = a(j,i) For weighted graphs, a(i,j) = w ij A D CB ABCD A0110 B1010 C1101 D0010

20 Representation Adjacency List: An array of vertices with pointers to linked lists of adjacent nodes, e.g., The size is O(E + V) so it is compact for sparse graphs. A D CB C B D B A C AC ABD C

21 6. Examples of Graph Algorithms Examples of Graph Algorithms: Graph Traversal Shortest Paths Minimum Cost Spanning Trees

Shortest Paths ( Dijkstra’s Algorithm ) Dijkstra's algorithm creates labels associated with vertices. These labels represent the distance (cost) from the source vertex to that particular vertex. Within the graph, there exists two kinds of labels: temporary and permanent.

Shortest Paths ( Dijkstra’s Algorithm ) The temporary labels (in circles) are given to vertices that have not been reached. The value given to these temporary labels can vary. Permanent labels (in squares) are given to vertices that have been reached and their distance (cost) to the source vertex is known. The value given to these labels is the distance (cost) of that vertex to the source vertex.

24 Shortest path (Example)

25 Shortest path (Example)

26 Shortest path (Example)

27 Shortest path (Example)

28 Shortest path (Example 2) find the shortest path from A to all other nodes using Dijkstra’s algorithm.

29 Shortest path (Example 2). Source destination lengthPath A D1(a,d) AE2(a,d,e) AC3(a,d,e,c) AB2(a,b) AF4(a,d,e,f)

Minimum Cost Spanning Trees (a) Spanning Tree Consider a connected undirected graph G(V,E). A sub-graph S(V,T) is a spanning tree of the graph (G) if: V(S) = V(G) and T  E S is a tree, i.e., S is connected and has no cycles

31 Spanning Tree S(V,T): V = {A,B,C,D,E,F,G} T = {AB,AF,CD,DE,EF,FG} FE GA D CB

32 Spanning Tree Notice that: |T| = |V| - 1 and adding any edge (u,v)  T will produce a cycle so that S is no longer a spanning tree (e.g. adding (G,D)) FE GA D CB

33 One Graph, Several Spanning Trees

34 (b) Minimum Cost Spanning Tree (MST) Consider houses A..F connected by muddy roads with the distances indicated. We want to pave some roads such that: We can reach a house from any other house via paved roads. The cost of paving is minimum. This problem is an example of finding a Minimum Spanning Tree (MST) E G A B C F D

35 Minimum Spanning Tree (MST) Cost: For a weighted graph, the cost of a spanning tree is the sum of the weights of the edges in that tree. Minimum Spanning tree: A spanning tree of minimum cost For the shown graph, the minimum cost is 22 E G A B C F D

36 Kruskal’s Algorithm for MST A Greedy Algorithm: Builds the MST edge by edge into a set of edges (T). At a given stage, chooses an edge that results in minimum increase in the sum of costs included so far in (T). The set (T) might not be a tree at all stages of the algorithm, but it can be completed into a tree iff there are no cycles in (T). hence the algorithm Builds up forests, then joins them in a single tree. Constraints: - The graph must be connected. - Uses exactly V-1 edges. - Excludes edges that form a cycle.

37 Abstract Algorithm - Form Set E of edges in increasing order of costs. - Set MST T = empty -Repeat Select an edge (e) from top. Delete edge from E set. Add edge (e) to (T) if it does not form a cycle, otherwise, reject. -Until we have V-1 successful edges.

38 Example E G A B C F D Edge uvw accept 1EF2 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6

39 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6

40 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6

41 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6

42 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4 6EG4 7DF5 8BD6

43 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 8BD6

44 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 8BD6

45 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 No 8BD6

46 Example E G A B C F D Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 NO 8BD6yes

47 Prime Algorithm for MST The algorithm add one vertex at a time. It starts at any vertex in a graph (vertex A, for example), and finds the least cost vertex (vertex B, for example) connected to the start vertex. Now, from either ‘A’ or ‘B’, it will find the next least costly connection, without creating a cycle, and you continue in this manner till all vertices are included.