Graph Theory.

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

CS203 Lecture 15.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CHAPTER 6 GRAPHS All the programs in this file are selected from
Review Binary Search Trees Operations on Binary Search Tree
1 Slides based on those of Kenneth H. Rosen Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Graphs.
Konigsberg Bridge Problem
Chapter 6. Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Data Structures Using C++
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graph Essentials Social Media Mining. 2 Measures and Metrics 2 Social Media Mining Graph Essentials Networks A network is a graph. – Elements of the network.
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.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
IS 2610: Data Structures Graph April 5, 2004.
Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
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,
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graph Theory and Applications
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
COSC 2007 Data Structures II
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Theory Graph Theory - History Leonhard Euler's paper on “Seven Bridges of Königsberg”, published in 1736.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures 13th Week
CS120 Graphs.
Graphs Chapter 13.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
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:

Graph Theory

The Koenigsberg Bridge Problem Bridges around the island Kneiphof The Koenigsberg bridge problem: Starting at some land area (in four areas), is it possible to return to our starting location after walking across each of the seven bridges exactly once? In 1763, Leonhard Euler solved the problem by using a graph (actually a multigraph) in which the land areas are vertices and the bridges are edges.

Euler’s Solution Euler defined the degree of a vertex as the number of edges incident on it. He then showed that there is a walk starting at any vertex, going through each edge exactly once, and terminating at the starting vertex iff the degree of each vertex is even. A walk that does this is called Eulerian. Graphs have been used in a wide variety of applications, including analysis of electrical circuits finding shortest routes project planning the identification of chemical compounds social network analysis. Graphs may be the most widely used of all mathematical structures.

The Graph Abstract Data Type A graph, G, consists of two sets: a finite, nonempty set of vertices, V(G), and a finite, possibly empty set of edges, E(G). We may write G=(V, E) to represent a graph. An undirected graph is one in which the pair of vertices representing any edge is unordered. E.g. (v0, v1)=(v1, v0) A directed graph is one in which we represent each edge as a directed pair of vertices. E.g. <v0, v1> represents an edge in which v0 is the tail and v1 is the head.

Sample Graphs 1 2 1 2 1 3 3 4 5 6 G1 2 G2 G3 complete graph 1 2 1 2 1 3 3 4 5 6 G1 2 G2 complete graph incomplete graph G3 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}

Complete Graph (Clique) A complete graph is a graph that has the maximum number of edges For undirected graph with n vertices, the maximum number of edges is n(n-1)/2 For directed graph with n vertices, the maximum number of edges is n(n-1) Examples 1 2 1 2 3 3

Adjacent and Incident If (v0, v1) is an edge in an undirected graph, v0 and v1 are adjacent The edge (v0, v1) is incident on vertices v0 and v1 If <v0, v1> is an edge in a directed graph v0 is adjacent to v1, and v1 is adjacent from v0 The edge <v0, v1> is incident on v0 and v1 Restrictions on graphs: A graph may not have an edge from a vertex, i, back to itself. Such edges are known as self loops. A graph may not have multiple occurrences of the same edge. If we remove this restriction, we obtain a data object referred to as a multigraph.

Subgraph A subgraph of G is a graph G’ such that V(G’)V(G) and E(G’)  E(G).

Path, Cycle, and Connected Component A path from vertex vp to vertex vq in a graph, G, is a sequence of vertices, vp, vi1, vi2, …, vin, vq,such that (vp, vi1), (vi1, vi2), …, (vin, vq) are edges in an undirected graph. The length of a path is the number of edges on it. A simple path is a path in which all vertices, except possibly the first and the last, are distinct. A cycle is a simple path in which the first and the last vertices are the same. In an undirected graph G, two vertices, v0, v1, are connected if there is a path in G from v0 to v1. A connected component, or simply a component, of an undirected graph is a maximal connected subgraph. A tree is a graph that is connected and acyclic.

Connected Components in Directed Graph Weakly Connected Component (WCC) Ignore directionality and find the connected components Strongly Connected Component (SCC) For all node-pairs, i and j, there is a path from node i to node j and there is a path from node j to node i Rescursively Strongly Connected Component (RSCC) For all node-pairs, i and j, the path from node i to node j and the path from node j to node i must be the same

Degree The degree of a vertex is the number of edges incident to that vertex For directed graph, the in-degree of a vertex v is the number of edges that have v as the head the out-degree of a vertex v is the number of edges that have v as the tail If di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is 3 2 1 2 in:1, out: 1 3 3 3 1 2 3 1 in: 1, out: 2 3 4 5 6 3 1 3 1 1 1 in: 1, out: 0 2

Adjacency Matrix Let G=(V,E) be a graph with n vertices. The adjacency matrix of G is a two-dimensional n by n array, say adj_mat If the edge (vi, vj) is in E(G), adj_mat[i][j]=1. If there is no such edge in E(G), adj_mat[i][j]=0. The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric. Merits of adjacency matrix To determine the connection of vertices is easy The degree of a vertex is For a digraph, the row sum is the out_degree, while the column sum is the in_degree Drawback of adjacency matrix for sparse graph, most of terms in the adjacency matrix equal zero.

Examples for Adjacency Matrix 1 2 3 4 5 6 7 1 2 3 1 2 G2 G1 symmetric undirected: n2/2 directed: n2 G4

Adjacency Lists Adjacency lists: replace the n rows of the adjacency matrix with n linked lists, one for each vertex in G. An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes. A directed graph with n vertices and e edges ==> n head nodes and e list nodes.

Interesting Operations for Adjacency Lists Degree of a vertex in an undirected graph # of nodes in adjacency list Number of edges in a graph determined in O(n+e) Out-degree of a vertex in a directed graph number of nodes in its adjacency list In-degree of a vertex in a directed graph traverse the whole data structure

Inverse Adjacency Lists Determine in-degree of a vertex in a fast way.  1 NULL 1 2 1 0 NULL 1 NULL 2

Lab 1: Representing a Network in UCINET friendship spouse friendship + spouse

Homework 1: Drawing a Ring of Ring 5*5: 4*4 4*5 5*4 3*6 6*3 …

Weighted Edges In many applications, the edges of a graph are assigned weights. These weights may represent the distance from one vertex to another or the cost of going from one vertex to an adjacent vertex. A graph with weighted edges is called a network. Example Cost adjacency matrix represents the airline network.

Elementary Graph Operations Recall four kinds of tree traversals: Preorder Inorder Postorder Level order To visit all vertices in G that are reachable from v, that is, all vertices that are connected to v. Depth-First Search (DFS) preorder Breadth-First Search (BFS) level order DFS and BFS can be used to find Connected components Spanning tree

Depth-First Search 1 2 3 4 5 6 7 Major steps: Consider example graph Visiting the start vertex, v. (that is, printing it) Select an unvisited vertex, w, from v’s adjacent nodes and carry out a depth first search on w. Preserve the current position in v’s adjacent nodes by placing it on a stack. Consider example graph Traversal order: 0, 1, 3, 7, 4, 5, 2, 6 Time complexity Adjacency lists: O(e) Adjacency matrix: O(n2) 1 2 3 4 5 6 7

Breadth-First Search 1 2 3 4 5 6 7 Major steps: Consider example graph Visiting the start vertex, v. (that is, printing it) Visits each of the vertices on v’s adjacent nodes. As we visit each vertex, we place the vertex in a queue. Consider example graph Traversal order: 0, 1, 2, 3, 4, 5, 6, 7 Time complexity Adjacency lists: O(e) Adjacency matrix: O(n2) 1 2 3 4 5 6 7

Connected Components in Undirected Graph In an undirected graph G, two vertices, v0, v1, are connected if there is a path in G from v0 to v1. A connected component, or simply a component, of an undirected graph is a maximal connected subgraph. We can find a connected component by either DFS or BFS. Time complexity: Adjacency lists: O(n + e) Adjacency matrix: O(n2)

Spanning Trees A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. We may use either DFS or BFS to create a spanning tree. When DFS is used, a depth first spanning tree is built. When BFS is used, a breadth first spanning tree is built. E(G): T (tree edges) + N (nontree edges) where T: set of edges used during search N: set of remaining edges While adding a nontree edge into any spanning tree, this will create a cycle.

DFS vs. BFS Spanning Tree 1 2 1 2 1 2 3 4 5 6 3 4 5 6 3 4 5 6 7 7 nontree edge cycle 7 DFS Spanning BFS Spanning

Minimal Subgraph A spanning tree is a minimal subgraph, G’, of G such that V(G’)=V(G) and G’ is connected. Any connected graph with n vertices must have at least n-1 edges. A biconnected graph is a connected graph that has no articulation points. 1 2 3 4 5 6 7

one connected component two connected components 8 9 8 9 8 9 7 1 2 a connected graph 3 5 4 6 one connected component two connected components 8 9 8 9 7 7 1 1 2 2 5 3 5 3 6 4 6 4

Biconnected Components Biconnected components: A maximal connected subgraph H no subgraph that is both biconnected and properly contains H 8 9 8 9 7 7 1 7 1 7 1 2 2 3 5 3 5 3 5 4 6 4 6 biconnected components

Finding the Binconnected Components Either by examining depth first spanning tree Or by UCINET…

Lab 2: Analyzing Connected Components Weakly Connected Components Strongly Connected Components Recursively Connected Components Bi-Connected Components

Shortest Paths Given a graph and two nodes, A and B, in the graph. V0 Is there a path from A to B? Is there is more than one path from A to B, which is the shortest path? Example: shortest paths from v0 to all destinations V0 V4 V1 V5 V3 V2 50 10 20 10 15 20 35 30 15 3 45 (a) path length 1) v0 v2 10 2) v0 v2 v3 25 3) v0 v2 v3 v1 45 4) v0 v4 45 (b)

Determining the Shortest Paths Main loops for (i=0; i<n; i++) { s[i] = FALSE; dist[i] = length[v][i]; } s[v] = TRUE; dist[v] = 0; for (i=0; i<n-2; i++) {//determine n-1 paths from v u = choose(n); //choose returns a value u such that: //dist[u]=minimum dist[w], where s[w]=FALSE s[u] = TRUE; for (w=0; w<n; w++) if (!s[w]) if (dist[u]+length[u][w]<dist[w]) dist[w] = dist[u]+length[u][w]; Time complexity: O(n2)

Example for the Shortest Paths Boston 4 1500 Chicago San Francisco Denver 250 1200 3 800 1000 1 2 New York 5 300 1000 1400 New Orleans 1700 900 Los Angeles 7 Miami 1000 6 0 1 2 3 4 5 6 7 0 0 1 300 0 2 1000 800 0 3 1200 0 4 1500 0 250 5 1000 0 900 1400 6 0 1000 7 1700 0 Length-adjacency matrix

Action of Finding the Shortest Paths (b) (c) (d) (a) (f) (e) (h) (g) (j) (i)

All-Pairs Shortest Paths Find the shortest paths between all pairs of vertices. Solution 1 Apply shortest path n times with each vertex as source. O(n3) Solution 2 Represent the graph G by its length-adjacency matrix with length[i][j]. If the edge <i,j> is not in G, the length[i][j] is set to some sufficiently large number. Ak[i][j] is the cost of the shortest path form i to j, using only those intermediate vertices with an index <= k. A-1[i][j]=length[i][j] Calculate the A0, A1, A2, ..., An-1 from A-1 iteratively. Ak[i][j]=min{Ak-1[i][j], Ak-1[i][k]+Ak-1[k][j]}, k>=0

Examples for All-Pairs Shortest Paths

Transitive Closure 1 2 3 4 (a) Digraph G (b) Adjacency matrix A for G Given a graph with unweighted edges, determine if there is a path from i to j for all i and j. (1) Require positive path (> 0) lengths. (2) Require nonnegative path (0) lengths. transitive closure matrix reflexive transitive closure matrix 1 2 3 4 1 2 3 4 (a) Digraph G (b) Adjacency matrix A for G 1 2 3 4 1 2 3 4 cycle reflexive (c) transitive closure matrix A+ (d) reflexive transitive closure matrix A*

Lab 3: Calculating Shortest Paths Shortest-path-length matrix Reflexive transitive closure matrix Dichotomizing shortest-path-length matrix