Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs - Definitions and Models Graphs can be used to model many computational problems. Graph theory has a well developed theory that can be put to effective.

Similar presentations


Presentation on theme: "Graphs - Definitions and Models Graphs can be used to model many computational problems. Graph theory has a well developed theory that can be put to effective."— Presentation transcript:

1 Graphs - Definitions and Models Graphs can be used to model many computational problems. Graph theory has a well developed theory that can be put to effective use in efficient algorithm design. Examples: traffic flow, road maps etc. scheduling problems chemical applications (organic molecules) relations among finite collection of entities(e.g. relational database) electrical circuits, VLSI, power lines etc. geometric problems

2 Graph - Definitions A graph is defined by two quantities: a finite set V (called vertices) and a finite subset E (called edges) of 2-element subsets of V. Example: V = {1,2,3,4}, E= {{1,2},{1,3},{1,4},{2,4}} 1 3 2 4

3 Directed Graphs, Paths, Connectivity etc. If the edges are ordered 2-element subsets (where {u,v} is different from {v,u}), then we call it a directed graph. We denote such edges by. Weighted graph has weights on edges. Path: A sequence of vertices v 1, v 2,..., v k is a path if all vertices are distinct and {v 1, v 2 }, {v 2, v 3 },...{v k-1, v k } are all in E. Cycle: A sequence of vertices v 1, v 2,..., v k is a cycle if vertices v 1 through v k-1 are distinct and {v 1, v 2 }, {v 2, v 3 },...{v k-1, v 1 } are all in E.

4 Connectivity A (directed) graph is (strongly) connected if there is a path between every pair of vertices. A graph is 2-connected or biconnected if the graph remains connected after removing any single vertex of the graph. A vertex is called an articulation point if its removal disconnects a graph. Theorem: A graph is biconnected if and only if every pair of vertices is in a cycle. 2 1 35 6 4 Is this graph biconnected? If not what are the articulation points?

5 k-connectivity We say that a graph is k-connected if the removal of any set of k-1 vertices does not disconnect the graph. A graph’s connectivity is k if it is k-connected but not (k+1)-connected. A graph with high connectivity is desirable since it will increase the robustness of the graph (as a communication network). But adding edges is expensive. So we want to minimize the number of edges, keeping a high connectivity. Or maximize connectivity for a given number of edges.

6 Spanning Tree Tree: A graph that is connected, but has no cycles. Facts: (a) A tree is maximally acyclic, i.e., adding any edge will introduce a cycle. (b) A tree on n vertices has exactly n-1 edges. Subgraph: A graph G’= is a subgraph of G= if V’ is a subset of V, and E’ is a subset of E’ and for every member {u,v} of E’, both u and v are in E’. Spanning subgraph: V’ = V. Induced subgraph: E’ = E intersection V’ X V’ A spanning subgraph that is a tree is called a spanning tree.

7 A simple application of spanning tree Consider a network of computers. Computers are denoted by nodes and connection by edges. Assume that the network is connected. Each computer has a message that is addressed to a specific computer. The goal is to route all the messages to the destination correctly. At each step, only one operation is allowed: a pair of computers can exchange their messages. Show that it is always possible to route messages under these conditions.

8 Maximum Matching in Bipartite Graphs A bipartite graph is a graph in which vertices are divided into two groups V1 and V2. An edge e = {u,v} can only connect a vertex in V1 to a vertex in V2. This is a natural model to represent a number of situations such as job/people, student/advisor, resident/town etc. An example of a bipartite graph is shown in the next slide.

9 Example: 1 2 3 4 5 a b c d e N(1) = {a,c} N(2) = {a,c} N(3) = {a,b,c,d} N(4) = {a,c} N(5) = {a,b,c,d,e}

10 Example: 1 2 3 4 5 a b c d e A matching in a graph is a set of edges such that no two of them share a common edge. The colored edges form a matching in the graph. A matching is maximum if there is no larger matching in the graph. If |V1|= |V2| = n, then no matching can be larger than n. A matching of size n will be called perfect matching.

11 1 2 3 4 5 a b c d e Claim: G does not have a perfect matching, i.e., a matching of size 5. Proof: Define N(S) as the set of neighbors of vertices in S. N({1,2,4}) = {a,c}. Since |N({1,2,4}| < |{1,2,4}|, the vertices 1,2 and 4 can’t all be matched. So no perfect matching. Weighted version of the problem also has important applications.

12 Network flow problem w Input description: a graph (network) G where each edge (v,w) has associated capacity c(v,w), and a specified source node s and sink node t w Problem description: What is the maximum flow you can route from s to t while respecting the capacity constraint of each edge? st 7 5 6 3 4 5 4 1 4 6 3 7

13 Coloring, independent set, vertex cover etc. Consider the problem of designing traffic lights at an intersection of several streets. Each traffic flow will be permitted for a fixed duration during a cycle. Say, 2 minutes. Our goal is to minimize the total length of each cycle which is equivalent to minimizing the number of slots in each cycle. Yellow path intersects red, but not blue.

14 Graph model for traffic light problem The main idea behind this optimization problem is that if two paths don’t intersect, traffic flow can proceed along those paths at the same time. Number the roads 1,2,… etc. The traffic flow directions, etc. are vertices of a graph. Add edge between two vertices if the paths intersect. Problem is to assign labels to vertices of the graph so that vertices with an edge connecting them are assigned different labels. Minimize the number of labels used. This is called graph coloring problem.

15 Other problems that can be modeled as coloring problem are: (a)Register allocation problem. (b) Scheduling problem: Courses to be offered next semester at SFSU: Art 101, 110, 121, 122, …, 899 Phil 100, 102, …, 597 Math 020, 075, …, 891. Csc 101, 102, 112, …, 875............................ Common intersections between (Art 101, Phil 100), (Csc 101, Phy 101), … Can we schedule the courses so that no two courses that have common intersection are offered at the same time?

16 Vertex cover, independent set, 3-dim. matching, Hamilton cycle etc. Let G= be a graph. A set of vertices V’ so that every edge has at least one end vertex included in V’ is called a vertex cover. A set V’ of vertices so that there is no edge between any pair of them is called an independent set. A path P in a graph G= is a Hamilton path if it goes through every vertex (exactly once). (A special case of traveling salesman problem.)

17 Typical optimization problem Input: A graph G= and integer k. Output: yes if G has a vertex cover of size at most k, no else. Such problems (with yes/no answers) are called decision problems. Similarly one can formulate decision problems such as: independent set vertex cover, coloring etc. Some problems such as Hamilton cycle, 3- dimensional matching are inherently decision problems.

18 Complexity classification: A problem instance is encoded using a standard encoding. For a positive integer k, let code(k) denote the binary representation of integer k. A graph G= be encoded as follows. code(n)# #code(e 1 )##code(e 2 )##...##code(e k ) where edge could be encoded as follows: If e ={u,v}, code(e) could be code(u)#code(v). Size of an encoding is the total number of bits needed to represent an encoding. For all the problems above, algorithm of time complexity O(c n) is obvious where n = input size.

19 Since exponential function grows very rapidly, usually algorithms of exponential time complexity are of limited use, say to solve only very small inputs. For some problems, the obvious approaches give exponential time algorithms, but some non-obvious approach gives a polynomial time algorithm. Example: 2-dimensional (weighted) matching, k-connectivity, flow with given value … For many problems in the above list, no polynomial time algorithm is known. How do we deal with such problems?

20 More realistic representations of graphs: adjacency matrix adjacency list incidence matrix They are all polynomially related to each other. Adjacency matrix has the following advantages: simple, array based. Constant time to decide if there is an edge between two given vertices in a RAM model. Disadvantages: most of the problems require at least  (n 2 ) steps to solve.

21 Depth-First Search Goal of DFS is to visit all the (reachable) vertices and traverse all the edges of a graph G =. It classifies edges into two groups: tree edges and back edges. Assuming that the graph is connected, tree edges will form a spanning tree. (Called the Depth First Spanning Tree ). Also assigns a number to each vertex. (Depth first number, or DFN.) Directed and undirected cases are different. We will consider the (easier) undirected case.

22 A recursive subroutine to explore the rest of the graph from a vertex, assuming that some vertices have been visited already. (A previously visited vertex is colored gray or black.) DFS_VISIT(u) { DFN [u] = CurNum++; Color[u] = gray; for every v adjacent to u do { if (Color[v] = = white) { Parent[v] = u; DFS_VISIT(v); } //end for color[u] = black; }

23 void DFS(Graph G) { for (j= 1; j <= G.VerSize; ++j) Color[j] = white; for (j= 1; j <= G.VerSize; ++j) if (Color[j]= = white) DFS_VISIT(j) } 1 3 4 5 6 2 Note the order of visit by DFS_VISIT depends on the adjacency list. DFN assigned will be: 1 2 3 4 5 6 DFN[ ] 1 6 4 3 5 2 Tree edges are shown in bold. Time complexity O(e+n) Storage O(n)

24 Applications of DFS Connectivity testing Shortest path in an unweighted graph. (Does not work in the weighted case.) Even in the unweighted case, BFS to be discussed next is probably simpler to use. Topological sorting. Advanced applications: bi-connectivity and tri-connectivity testing. Planarity testing.

25 Topological Sorting Goal is to arrange the vertices of a directed, acyclic graph so that if is an edge, then u appears before v. Algorithm: Run DFS algorithm. (Note that you need the directed version of the algorithm.) When DFS_VISIT returns from a vertex, that vertex is inserted in front of a linked list.

26 Breadth-First Search This is a queue-based algorithm. Usually it is implemented as a nonrecursive algorithm. It is also called Level-order Searching when applied to a (rooted) tree, whereas the more familiar searches (in-order, pre-order and post-order) are all variations of Depth-first Search. Applications: connectivity reachability shortest-path in an un-weighted graph, etc.

27 BFS_VISIT(Graph G, vertex u) { for every v in G Color[v] = white; Initialize a queue Q; Insert(Q,u); BFNum = 1; while (Q.notEmpty( )) { t = REMOVE(Q); BFN[t] = BFNum++; Color[t] = black; for every vertex v adjacent to t if (Color[v] = = white) INSERT(Q,v); } Time complexity = O(e+n). Storage requirement O(n) (in addition to the input size which is O(e+n).)

28 Shortest Path Algorithm Given a weighted, directed graph, and a vertex u, find the shortest path from u to every other vertex? Note that triangle-inequality may not hold. If triangle inequality holds, then DFS or BFS can be used and hence the problem can be solved in time = O(e+n). Assumption: Edge weights are positive. 12 5 4

29 Dijkstra’s algorithm: Input G =. w is the edge weight, and u is the source. At any stage, S is the set of vertices such that the shortest distance from u to every vertex s in S is known. (Initially, S = {u}.) The algorithm performs n-1 iterations. In each iteration, size of S increases exactly by 1. In the end |S| = n and the problem is solved. We will now focus on a single iteration step.

30 We don’t need to keep track of S. What we need is a data structure to store S’ (the complement of S). More precisely, for each vertex in S’, the shortest path length from u to that vertex that only goes through vertices in S is stored in a binary heap. Essence of Dijkstra’s algorithm is how to update this data structure when a vertex is removed from S’. Dijkstra’s algorithm is a series of n-1 iterations (where n is the number of vertices) in each of which one vertex is moved from S’ to S.

31 Update( ) { v = DeleteMin(Heap); //v is the next vertex to enter S. for every vertex u in S’ such that is in E if (Cost[v] + w < Cost[u]) UpdateHeap(u, Cost[v] + w ) } UpdateHeap takes as input the index of a node in a heap and new key value. It changes the value of this node to the new key (and restores the heap property).

32 We will show in class that the total cost of implementing the above algorithm is O(e log n). The storage requirement is O(n). We assume that the input is stored using adjacency list representation.


Download ppt "Graphs - Definitions and Models Graphs can be used to model many computational problems. Graph theory has a well developed theory that can be put to effective."

Similar presentations


Ads by Google