# Graph Theory.

## Presentation on theme: "Graph Theory."— 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.

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.

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

Determine in-degree of a vertex in a fast way. NULL 1 2 1 NULL 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
7 1 2 a connected graph 3 5 4 6 one connected component two connected components 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 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 45 (a) path length 1) v0 v 2) v0 v2 v 3) v0 v2 v3 v1 45 4) v0 v (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 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