Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Graph Theory. 2 The Koenigsberg Bridge Problem Bridges around the island Kneiphof The Koenigsberg bridge problem: Starting at some land area (in four.

Similar presentations


Presentation on theme: "1 Graph Theory. 2 The Koenigsberg Bridge Problem Bridges around the island Kneiphof The Koenigsberg bridge problem: Starting at some land area (in four."— Presentation transcript:

1 1 Graph Theory

2 2 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.

3 3 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.

4 4 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. (v 0, v 1 )=(v 1, v 0 ) A directed graph is one in which we represent each edge as a directed pair of vertices. E.g. represents an edge in which v 0 is the tail and v 1 is the head.

5 5 Sample Graphs G1G1 G2G2 G3G3 V(G 1 )={0,1,2,3} E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G 2 )={0,1,2,3,4,5,6} E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G 3 )={0,1,2} E(G 3 )={,, } complete graph incomplete graph

6 6 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

7 7 Adjacent and Incident If (v 0, v 1 ) is an edge in an undirected graph, v 0 and v 1 are adjacent The edge (v 0, v 1 ) is incident on vertices v 0 and v 1 If is an edge in a directed graph v 0 is adjacent to v 1, and v 1 is adjacent from v 0 The edge is incident on v 0 and v 1 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.

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

9 9 Path, Cycle, and Connected Component A path from vertex v p to vertex v q in a graph, G, is a sequence of vertices, v p, v i 1, v i 2, …, v i n, v q,such that (v p, v i 1 ), (v i 1, v i 2 ), …, (v i n, v q ) 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, v 0, v 1, are connected if there is a path in G from v 0 to v 1. 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.

10 10 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

11 11 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 d i is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is in:1, out: 1 in: 1, out: 2 in: 1, out: 0

12 12 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 (v i, v j ) 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.

13 13 Examples for Adjacency Matrix G1G1 G2G2 G4G symmetric undirected: n 2 /2 directed: n 2

14 14 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.

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

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

17 17 Lab 1: Representing a Network in UCINET friendship friendship + spousespouse

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

19 19 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.

20 20 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

21 21 Depth-First Search Major steps: 1. Visiting the start vertex, v. (that is, printing it) 2. Select an unvisited vertex, w, from v’s adjacent nodes and carry out a depth first search on w. 3. 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(n 2 )

22 22 Breadth-First Search Major steps: 1. Visiting the start vertex, v. (that is, printing it) 2. Visits each of the vertices on v’s adjacent nodes. 3. 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(n 2 )

23 23 Connected Components in Undirected Graph In an undirected graph G, two vertices, v 0, v 1, are connected if there is a path in G from v 0 to v 1. 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(n 2 )

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

25 25 DFS vs. BFS Spanning Tree DFS Spanning BFS Spanning nontree edge cycle

26 26 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

27 a connected graph two connected components one connected component

28 28 Biconnected Components Biconnected components: A maximal connected subgraph H no subgraph that is both biconnected and properly contains H biconnected components

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

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

31 31 Shortest Paths Given a graph and two nodes, A and B, in the graph. 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 v 0 to all destinations V0V0 V4V4 V1V1 V5V5 V3V3 V2V (a) path length 1) v0 v2 10 2) v0 v2 v3 25 3) v0 v2 v3 v1 45 4) v0 v4 45 (b)

32 32 Determining the Shortest Paths Main loops for (i=0; i

33 33 Example for the Shortest Paths San Francisco Denver Chicago Boston New York Miami New Orleans Los Angeles Length-adjacency matrix

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

35 35 All-Pairs Shortest Paths Find the shortest paths between all pairs of vertices. n Solution 1 Apply shortest path n times with each vertex as source. O(n 3 ) n Solution 2 Represent the graph G by its length-adjacency matrix with length[i][j]. If the edge is not in G, the length[i][j] is set to some sufficiently large number. A k [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 A 0, A 1, A 2,..., A n-1 from A -1 iteratively. A k [i][j]=min{A k-1 [i][j], A k-1 [i][k]+A k-1 [k][j]}, k>=0

36 36 Examples for All-Pairs Shortest Paths

37 37 Transitive Closure 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 (a) Digraph G (b) Adjacency matrix A for G (c) transitive closure matrix A + (d) reflexive transitive closure matrix A * cycle reflexive

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


Download ppt "1 Graph Theory. 2 The Koenigsberg Bridge Problem Bridges around the island Kneiphof The Koenigsberg bridge problem: Starting at some land area (in four."

Similar presentations


Ads by Google