GRAPH ALGORITHMS Week 3 (16-21 October 2017) C. Croitoru croitoru@info.uaic.ro FII October 15, 2017 1 / 63
OUTLINE Graph Theory Vocabulary 1 Definition of a Graph 2 Variations in the Definition of a Graph 3 Degrees 4 Subgraphs 5 Graph Operations 6 Graph Classes 7 Paths and Circuits 8 Associated Matrices 9 Data Structures Path Problems in (Di)Graphs 1 (Di)Graph Traversal 2 Shortest Paths Problems 3 Connectivity Exercises for the next week Seminar (23-28 October 2017) 2 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Walk of length r from v to w in G: any sequence of vertices and edges of the form (v =)v 0, v 0 v 1, v 1,..., v r 1, v r 1 v r, v r (= w). v and w are the extremities of the walk. Trail: a walk with distinct edges. Path: a walk with distinct vertices. Remark. A vertex is walk (trail, path) of length 0. 3 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Walk of length r from v to w in G: any sequence of vertices and edges of the form (v =)v 0, v 0 v 1, v 1,..., v r 1, v r 1 v r, v r (= w). v and w are the extremities of the walk. Trail: a walk with distinct edges. Path: a walk with distinct vertices. Remark. A vertex is walk (trail, path) of length 0. Example 2 1 6 3 mers M: 4 parcurs 1 T: 2 8 2 3 6 5 8 4 9 1 9 2 8 8 5 8 9 5 drum P: 6 3 2 8 9 4 4 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Closed walk: a walk from v to v. Circuit (Cycle, Closed path): a walk with distinct vertices except their extremities which are equal. A circuit is even or odd depending on the parity of its length. The length of the shortest circuit (if any) is the girth, g(g), of G. The maximum length of a circuit is the circumference, c(g), of G. 5 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Closed walk: a walk from v to v. Circuit (Cycle, Closed path): a walk with distinct vertices except their extremities which are equal. A circuit is even or odd depending on the parity of its length. The length of the shortest circuit (if any) is the girth, g(g), of G. The maximum length of a circuit is the circumference, c(g), of G. Example 2 1 6 3 4 circuite impare : 1 2 9 8 4 9 8 4 8 9 5 circuit par : 1 9 2 8 6 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Distance in G from v to w, d G (v, w): the length of the shortest path in G from v to w. Diameter of the graph G, d(g): d(g) = max{d G (v, w) v, w V (G)}. d(g)=3 d(g)=4 7 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Distance in G from v to w, d G (v, w): the length of the shortest path in G from v to w. Diameter of the graph G, d(g): d(g) = max{d G (v, w) v, w V (G)}. d(g)=3 d(g)=4 D = (V, E) digraph All the above definitions are preserved by considering directed edges (arcs) instead of edges. 8 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Connected graph: there is a path between every pair of vertices. Otherwise, the graph is disconnected. Connected component of a graph G: a maximal connected subgraph H of G (there is no connected subgraph H of G, H H, and H is a subgraph of H ). Every graph can be expressed as a disjoint union of its connected components. If ρ V V, given by (v, w) ρ if and only if there is a path in G from v to w, then ρ is an equivalence relation, and the connected components of G are the subgraphs induced by the equivalence classes of ρ. Example. 4 connected components 9 / 63
Vocabulary 7. Path and Circuits D = (V, E) digraph Connected digraph (weakly connected): its support graph G(D) is connected. Unilaterally connected digraph: there is a path from v to w or from w to v, for any two vertices v and w. Strongly connected digraph: there is a path from v to w, for any two vertices v and w. Unilateral conex Tare conex Conex 10 / 63
Vocabulary 7. Path and Circuits A Tree is a connected graph without circuits. A graph whose connected components are trees is a forest. Cut vertex: a vertex v, in the connected graph G, such that G v is not connected. Vertex cutset: a set S V (G), in the connected graph G, such that G S is not connected. Pct. de articulatie Fara pcte. de articulatie Multime de articulatie Fara multimi de articulatie 11 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph For p Z >0, G is a p-connected graph if V (G) = p and G = K p or V (G) p + 1 and G has no vertex cutset of cardinality less than p. Clearly, G is 1-connected if and only if it is connected. The vertex-connectivity number, k(g), of the graph G is k(g) = max{p Z >0 G is p-connected}. k(g)=3 k(g)=4 12 / 63
Vocabulary 7. Path and Circuits G = (V, E) graph Cut edge: an edge e, in the connected graph G, such that G e is not connected. Edge-cutset: a set S E(G), in the connected graph G, such that G S is not connected. For p Z >0, G is a p-edge-connected graph if G has no edge-cutset of cardinality less than p. The edge-connectivity number, λ(g), of G: λ(g) = max{p Z >0 G is p-edge-connected}. Punte Multime separatoare de muchii lambda(g)=3 13 / 63
Vocabulary 7. Path and Circuits G (di)graph is Eulerian if there is a closed trail in G passing through each edge of G. Hamiltonian if there is a circuit in G passing through each vertex of G. Polynomial time recognition of Eulerian (di)graphs (Euler, 1736). 14 / 63
Vocabulary 7. Path and Circuits G (di)graph is Eulerian if there is a closed trail in G passing through each edge of G. Hamiltonian if there is a circuit in G passing through each vertex of G. Polynomial time recognition of Eulerian (di)graphs (Euler, 1736). Examples 11 10 3 1 2 6 5 7 8 9 4 Graf Eulerian Graf care nu-i hamiltonian 8 5 1 4 7 6 2 3 Graf hamiltonian 15 / 63
Vocabulary 7. Path and Circuits Hamiltonian problems HAM Instance: G a graph. Question: Is G hamiltonian? NP-complete (Karp, 1972). NH Instance: G a graph. Question: Is it true that G is not hamiltonian? NH NP? 16 / 63
Vocabulary 7. Path and Circuits Hamiltonian problems HAM Instance: G a graph. Question: Is G hamiltonian? NP-complete (Karp, 1972). NH Instance: G a graph. Question: Is it true that G is not hamiltonian? NH NP? Non-hamiltonian graph: 3-conex, planar, si nehamiltonian (Tutte) 17 / 63
Vocabulary 8. Associated Matrices Adjacency Matrix Let G = ({v 1,..., v n }, {e 1,..., e m }) be a graph with a fixed ordering of vertex/edge set. The adjacency matrix of G is the matrix A = (a ij ) n n, where { 1 if v i and v j are adjacent a ij = 0 otherwise. 18 / 63
Vocabulary 8. Associated Matrices Adjacency Matrix Let G = ({v 1,..., v n }, {e 1,..., e m }) be a graph with a fixed ordering of vertex/edge set. The adjacency matrix of G is the matrix A = (a ij ) n n, where { 1 if v i and v j are adjacent a ij = 0 otherwise. Example. A graph and its adjacency matrix. 1 1 7 2 3 4 6 5 4 5 2 3 0 1 1 0 0 1 0 0 1 1 A = 1 0 0 1 1 0 1 1 0 1 0 1 1 1 0 19 / 63
Vocabulary 8. Associated Matrices Incidency Matrix Let G = ({v 1,..., v n }, {e 1,..., e m }) be a graph with a fixed ordering of vertex/edge set. The incidency matrix of G is the matrix B = (b ij ) n m, where { 1 if e j is incident with v i and e j b ij = 0 otherwise. 20 / 63
Vocabulary 8. Associated Matrices Incidency Matrix Let G = ({v 1,..., v n }, {e 1,..., e m }) be a graph with a fixed ordering of vertex/edge set. The incidency matrix of G is the matrix B = (b ij ) n m, where { 1 if e j is incident with v i and e j b ij = 0 otherwise. Example. A graph and its incidency matrix. 1 1 7 2 3 4 6 5 4 5 2 3 0 0 0 0 0 1 1 1 0 1 0 0 0 1 B = 0 1 0 1 0 1 0 0 0 1 1 1 0 0. 1 1 0 0 1 0 0 21 / 63
Vocabulary 8. Associated Matrices Digraphs For digraphs, similar matrices can be defined with entries in { 1, 0, 1} in order to point out the direction of edges. 22 / 63
Vocabulary 8. Associated Matrices Digraphs For digraphs, similar matrices can be defined with entries in { 1, 0, 1} in order to point out the direction of edges. Spectral theory The eigenvalues, eigenvectors and the characteristic polynomial of the adjacency matrix are called eigenvalues, eigenvectors, respectively characteristic polynomial of the graph. 23 / 63
Vocabulary 9. Data Structures Adjacency matrix Let G = (V, E) be a (di)graph with V = {1, 2,..., n} and E = e. If A = (a ij ) n n is the adjacency matrix of G then, representing it as a 2-dimensional array, we need O(n 2 ) time for initialization. Hence any algorithm that represents G with the adjacency matrix has Ω(n 2 ) time (and space) complexity. Testing if two vertices are adjacent is done in O(1) time, but passing through the set of neighbors N G (v) (or N + G (v)), for an arbitrary vertex v V, needs Ω(n) time unpractical in large sparse (di)graphs. 24 / 63
Vocabulary 9. Data Structures Adjacency List Let G = (V, E) be a (di)graph with V = {1, 2,..., n} and E = e. Every vertex v V has a list A(v) of its neighbors in G. If G is a graph then A(v) contains N G (v) = {w w V and vw E} and if G is a digraph then A(v) contains N + G (v) = {w w V and vw E}. If G is a graph then each edge vw E generates 2 nodes in the adjacency lists (one in A(v) and the other in A(w)): the space needed is O(n + 2e). If G is a digraph then the space needed is O(n + e). Adjacency lists can be implemented using linked lists or using arrays. Testing if a vertex v is adjacent to another vertex w in G needs Ω(d G (v)) time, but passing through the set of neighbors N G (v) (or N + G (v)), for an arbitrary vertex v V, can be done in Ω(d G (v)) time (and not in O(n) time as in the case of adjacency matrix). 25 / 63
Vocabulary 9. Data Structures Adjacency List Example. Adjacency list representation of the digraph below. 1 A(1) 3 2 2 3 A(2) 5 3 5 6 A(3) 6 A(5) A(6) 26 / 63
PATH PROBLEMS IN (DI)GRAPHS 27 / 63
Path Problems Graph traversal Graph traversal Graph traversal or graph search is an algorithmic paradigm specifying a systematic method to pass through the set of vertices reachable by paths starting from a specified vertex in a (di)graph. Given a (di)graph G = ({1,..., n}, E) and s V (G), generate efficiently the set S = {v v V (G) and there is a path from s to v}. G is represented with adjacency lists, because, during the visiting process, we need to handle in a efficient way the set of neighbors of the current vertex. 28 / 63
Path Problems Graph traversal BFS Breadth-First Search G = ({1,..., n} ( d i ) graph, s {1,..., n} Each vertex v has an integer label label(v) < 0 label(s) 0 ; parent(s) 0 ; create queue Q containing s ; while Q do { let v be the top vertex in the queue Q ; delete the top vertex in Q ; for w A(v) do if label(w) < 0 then { label(w) label(v) + 1 ; parent(w) v ; insert w into the queue Q } } 29 / 63
Path Problems Graph traversal BFS Breadth-First Search Properties. It is not difficult to prove that: S = {v V label(v) 0}. v V label(v) = d G (s, v) (distance in G from s to v); Variable parent defines the bfs-tree associated to the search from s: if G is a graph then the bfs-tree is a spanning tree of the connected component containing s; if G is a digraph then the bfs-tree is an arborescence (directed rooted tree in which all edges point away from the root s). The time complexity of BFS(s) is O(n S + m S ), where n S = S V = n, and m S = E([S] G ) E (this follows easily by observing that each node in the adjacency list of a vertex from S is accessed exactly once). 30 / 63
Path Problems Graph traversal BFS Breadth-First Search Example: 1 A(1) 3 2 0 1 2 0 2 3 A(2) 5 3 1 3 1 2 5 1 3 1 5 6 A(3) 6 A(5) 2 6 2 5 6 2 A(6) 31 / 63
Path Problems Graph traversal DFS Depth-First Search G = ({1,..., n} ( d i ) graph, s {1,..., n} Each vertex v has an integer label label(v) < 0 Each adjacency list A(v) will be traversed using an iterator next[a(v)] label(s) 0 ; parent(s) 0 ; create stack S containing s ; n S 0 ; while S do { let v be the top vertex in the stack S ; w next[a(v)] ; if w exists then if label(w) < 0 then { label(w) n S ; n S ++ ; parent(w) v ; insert w into the stack S } else NOP can be used!! else delete v from (the top of) S the search from v is finished } 32 / 63
Path Problems Graph traversal DFS Depth-First Search Properties. It is not difficult to prove that: {v V label(v) 0} is exactly the set S of the vertices reachable by paths from s in G. v V label(v) = visiting time of v (s has visiting time 0); Variable parent defines the dfs-tree associated to the search from s. The time complexity of DFS(s) is O(n S + m S ), where n S = S V = n, and m S = E([S] G ) E (this follows easily by observing that each node in the adjacency list of a vertex from S is accessed exactly once). 33 / 63
Path Problems Graph traversal DFS Depth-First Search Example: 1 A(1) 3 2 0 1 2 0 2 3 A(2) 5 3 1 3 3 2 5 1 3 2 5 6 A(3) 6 A(5) 2 6 4 5 6 3 A(6) 34 / 63
Path Problems Shortest Paths Notations Let G = (V, E) be a digraph, with V = {1,..., n}. Each directed edge e E has associated a cost a(e) R (weight, length,...). If G is represented with adjacency lists, then a(ij) is a field in the node of adjacency list of i (reprenting ij). For ease of notation we will use the representation of G with the cost-adjacency matrix A = (a ij ) n n, { a(ij) if ij E a ij = otherwise Here, denotes a big real number with respect to actual edge costs (e.g., > n max ij E a(ij)) and we suppose that a =, + =. (It is also possible to use as an unsuccessful access to the data structure used to represent the matix A). 35 / 63
Path Problems Shortest Paths Notations For i, j V, the set of all paths in G from i to j is denoted by P ij : P ij = {P ij P ij path in G from i to j}. If P ij P ij, P ij : (i =)v 0, v 0 v 1, v 1,..., v r 1, v r 1 v r, v r (= j), its set of vertices is V (P ij ) = {v 0, v 1,..., v r } and its set of arcs (directed edges) is E(P ij ) = {v 0 v 1, v 1 v 2,..., v r 1 v r }. Any vertex k V (P ij ), k i, j, splits P ij in two paths P ik P ik and P kj P kj. We denote P ij = P ik P kj. The cost of a path P ij P ij is a(p ij ) = 0 + In particular, we have a(p ii ) = 0. uv E(P ij ) a uv. 36 / 63
Path Problems Shortest Paths Main Shortest Path Problems Single-pair shortest path problem. P1: Given G digraph; a : E(G) R; s, t V (G), s t. Find P st P st, such that a(p st) = min{a(p st ) P st P st }. Single-source shortest path problem. P2: Given G digraph; a : E(G) R; s V (G). Find P si P si, i V (G), such that a(p si ) = min{a(p si) P si P si }. All-pairs shortest path problem. P3: Given G digraph; a : E(G) R. Find P ij P ij i, j V (G), such that a(p ij ) = min{a(p ij) P ij P ij }. 37 / 63
Path Problems Shortest Paths Main Shortest Path Problems Remarks. 1 The cost-adjacency matrix representation of the pair G, a implies that P ij i, j V : if a(p ij ) < then P ij is a true path in G and if a(p ij ) = then P ij is not a path in G but it is a path in the complete symmetric digraph obtained from G by adding all missing arcs (with costs). It follows that all sets over which a minimum cost element is required in the problems P1-P3 are non-empty and finite and all minimum paths required are well-defined. 2 The algorithms for solving the problem P1 are obtained from those solving the problem P2 by adding an (obvious) stopping test. 3 The problem P3 can be solved by iterating any algorithm for the problem P2. We ll see that there are more efficient solutions. 38 / 63
Path Problems Shortest Paths Applications 1. Communication Networks. The digraph G = (V, E) represents a communication network between the nodes in V and with E modeling the set of directed links between nodes. If a(e) 0 (for all e E) represents the length of the direct connection between the extremities of e, then the problems P1-P3 are natural shortest paths problems. If a(e) 0 (for all e E) represents the time needed for the direct connection between the extremities of e, then the problems P1-P3 are natural fastest paths problems. If a(e) (0, 1] (for all e E) represents the probability that the direct connection between the extremities of e works properly, and we suppose that edges works properly independent of each other, then the problems P1-P3 become most reliable paths problems: 39 / 63
Path Problems Shortest Paths Applications If P ij P ij for some pair i, j V, then the probability that this path works properly is (by the independence assumption) Prob(P ij ) = By taking a (e) := log(a(e)), log(prob(p ij )) = log( e E(P ij ) e E(P ij ) a(e). a(e)) = e E(P ij ) a (e). By the monotony of the log function it follows that the problems P1-P3, with costs a, give the most reliable paths in the communication network. 40 / 63
Path Problems Shortest Paths Applications 2. PERT Networks Critical Path Method (CPM). PERT (Project Evaluation and Review Technique) is a method to analyze (especially) the completion time of each task in a given complex project. Let P = {A 1,..., A n } be the set of atomic activities of a large project P (n is big). (P, <) is a partially ordered set, where A i < A j if i j and activity A j can be started only after the activity A i was finished. For each activity A i, its completion time t i is given (estimated). Find a scheduling of the activities of the project to minimize its total completion (calendar) time. We can associate a directed acyclic graph to the problem in this way: to each activity A p (p {1,..., n} we add an arc i p j p with cost a(i p j p ) = t p. The node i p corresponds to the beginning event of A p and the node j p is associated to the finishing event of it. If an activity A k can start immediately after the activity A p we add the arc j p i k (dummy activity). 41 / 63
Path Problems Shortest Paths Applications The construction of digraph is finished after adding a node s corresponding to the start event of the project linked by arcs si p for each activity A p with no incoming arcs, and a node t corresponding to the terminal event of the project linked by arcs j p t for each activity A p with no outgoing arcs. In the digraph obtained, the maximum cost of a path from s to t is equal to the minimum completion time of the project. A maximum cost path is called a critical path since any delay of an activity on this path infers a delay of the whole project. Start 0 0 0 A1: t1 A2: t2 A3: t3 0 A4: t4 0 0 0 0 0 A5: t5 A6: t6 A7: t7 0 0 0 A8: t8 A9: t9 0 0 A10:t10 0 End 0 42 / 63
Path Problems Shortest Paths Applications Knapsack Problem (0-1) We are given a knapsack of size b Z +, and n objects of sizes a 1,..., a n Z +. Also is known the profit p i Z + of inserting the object i (i {1,..., n} into the knapsack. We are asked to choose a filling of the knapsack of maximum total profit. Let x i {0, 1}, for i {1,..., n}, be a boolean variable having the meaning that x i = 1 if and only if the object i is inserted into the knapsack. Then the knapsack problem can be stated as max { n p i x i i=1 n a i x i b, x i {0, 1} i = 1, n }. i=1 Let G = (V, E) the digraph with V = {s} V 1 V 2... V n {t}, where the set V i = {i 0, i 1,..., i b } is associated to object i, i = 1, n. 43 / 63
Path Problems Shortest Paths Applications The arcs of G and their costs are: s1 0 and s1 a 1 with a(s1 0 ) = 0 and a(s1 a 1 ) = p 1 ( either the object 1 is inserted in the knapsack with profit p 1 and filling level a 1, or it is not inserted, with the profit and fitting level 0). i = 2, n j = 0, b: (i 1) j i j with a((i 1) j i j ) = 0 (the object i is not inserted into knapsack: from the filling with the first i 1 objects and filling level j, we pass to a filling with the first i objects, without object i; the filling level remains j and the additional profit is 0). If j a i 0 then we have also the arc (i 1) j a i i j with a ( (i 1) j a i i j) = p i (we can arrive at the filling level j by inseting the object i to a filling with the first i 1 objects, with the filling level j a i ). j = 0, b: n j t with a(n j t) = 0. 44 / 63
Path Problems Shortest Paths Applications Remark. Each path from s to t in G corresponds to a subset of objects with the filling level b and with the total profit equal to the cost of the path. Since, conversely, to each filling of the knapsack corresponds a path from s to t in G, it follows that the knapsack problem can be solved by finding a path of maximum cost in the directed acyclic graph G. a 0 0 s 1:0 2:0 n:0 p2 0 p1 1:1 2:1 n:1 0 0 2:a2 n:2 0 0 1:a1 2:a1 t p2 2:a1+a2 0 0 1:b 2:b n:b a The static description given above for G can be transformed into a procedural one, giving the usual dynamic programming solution. Note that the problem is NP-hard (the order of G could be exponential in the input size of 45 / 63
Path Problems Shortest Paths Solving P2 P2: Given G digraph; a : E(G) R; s V (G). Find P si P si, i V (G), s.t. a(p si ) = min{a(p si) P si P si }. 46 / 63
Path Problems Shortest Paths Solving P2 P2: Given G digraph; a : E(G) R; s V (G). Find P si P si, i V (G), s.t. a(p si ) = min{a(p si) P si P si }. Theorem 1. Let G = (V, E) be a digraph, V = {1,..., n}, s V and a : E R, s.t. (I ) a(c) > 0, for all C circuit in G. Then, (u 1,..., u n ) is a solution of the nonlinear system of equations { us = 0 (B) u i = min (u j + a ji ) i s. j i if and only if i V, P si P si s.t. a(p si ) = u i and a(p si ) = min{a(p) P P si}. 47 / 63
Path Problems Shortest Paths Proof of Theorem 1. Let Psi optimal solutions of P2 and u i = a(psi ) (i V ). The hypothesis (I) implies that u s = 0, i.e. the first equation of the system (B) is satisfied. For i s, the path Psi has a penultimate vertex j. If P sj is the path from s to j determined on Psi by j, we have u i = a(p si) = a(p sj ) + a ji a(p sj) + a ji = u j + a ji. We show that u i = u j + a ji. Suppose that u i > u j + a ji, i.e. a(p sj ) > a(psj ). Case 1. i V (Psj ). Then P1 = Psj (j, ji, i) P si and a(p 1 ) = a(psj ) + a ji < a(p sj ) + a ji = a(psi ), a contradiction (P si is a shortest path). D sj s D* sj j i 48 / 63
Path Problems Shortest Paths Proof of Theorem 1. Case 2. i V (Psj ). Let P sj = P si P ij the two paths determined by the vertex i on Psj. Then the cost of the circuit C = P ij (j, ji, i) is a(c) = a(p ij ) + a ji = a(psj ) a(p si) + a ji = u j + a ji a(p si ) u j + a ji a(psi ) = u j + a ji u i < 0, a contradiction (the hypothesis (I) is D violated). ij D sj C s D* sj j i Hence the part of the theorem is proved. Remark. We proved above that if j is the vertex before i on a shortest path from s to i, then the path from s to j determined by j on this shortest paths is a shortest path from s to j. Inductively, it follows: Bellman s Principle of Optimality: If P si is a shortet path from s to i, then j V (P si ), if dacă P si = P sj P ji then P sj (respectively P ji ) is a shortest path from s to j (respectively from j to i). 49 / 63
Path Problems Shortest Paths Proof of Theorem 1. We show that if (u 1,..., u n ) is a solution of (B), then (a) P si P si such that u i = a(p si ), i V. (b) i V, u i = min{a(p) P P si }(= a(p si )). (a) If i = s, then u s = 0 and the path P ss satisfies a(p ss ) = 0 = u s. If i s, let us consider the following algorithm v i; k 0; while v s do { find w such that u v = u w + a wv ;// w since u v satisfies (*) i k v; k + +; v w } i k s The algorithm find the path P : (s =)i k+1, i k+1 i k,..., i 1, i 1 i 0, i 0 (= i) with P P si and a(p) = a(i k+1 i k ) + + a(i 1 i 0 ) = (u ik u ik+1 ) + (u ik 1 u ik ) + + (u i0 u i1 ) = u i0 u ik+1 = u i u s = u i. In each while iteration w {i 0,..., i k 1 } (else we get a circuit of cost 0, violating the hypothesis (I)). 50 / 63
Path Problems Shortest Paths Proof of Theorem 1. Note that (with the notations in the above algorithm ) we have u i = u i1 + a i1i. (b) Let u i = a(p si ) i V. By the part of the proof, u i, i = 1, n, satisfy the system of equations (B). Suppose that u = (u 1,..., u n ) u = (u 1,..., u n ). Since u s = u s = 0, it follows that there is i s such that u i u i and j V (P si ), j i, u j = u j, where P si is the path constructed in (a) for u i. Then u i > u i = u i1 + a i1 i = u i1 + a i1 i u i ( the first inequality holds by the choice of i, the second holds since u i satisfies (B)). The contradiction found shows that u = u, that is the components of u are shortest paths costs. 51 / 63
Path Problems Shortest Paths Remarks From the above proof it follows that for solving P2 is sufficiently to found a solution of the system of equations (B). The corresponding shortest paths can be obtained as in part (a) of the proof: if we have u i = u k + a ki then k is the vertex before i on the shortest path from s to i (of cost u i ). In the algorithm that solves (B) we maintain an array before[1..n] with entries from V {0, } with the final meaning before[i]= the vertex before i on the shortest path from s to i. The vertices of this path can be found in O(n) time by constructing the sequence i, before[i], before[before[i]],..., s. If the algorithms solving the system of equations (B) circumvent (by the maintenance of the array before) 0-cost circuits, then the problem P2 is solved, even the unicity of the solution is lost. Hence these algorithms will solve P2 in the hypothesis (I ) a(c) 0, C circuit in G. 52 / 63
Path Problems Shortest Paths Remarks If, in the problems P1-P3, G is a graph and not a digraph, we can use the algorithms for digraphs by replacing each (undirected) edge of G with a symmetric pair of arcs, each having the cost of the edge. Note that this approach works only for non-negative costs of the edges (if an edge has negative cost, then the 2-circuit formed by the two symmetric arcs replacing the edge has negative cost, hence hypothesis (I ) is not satisfied). Since the sets P ij are finite (and non-empty), we can consider problems similar to P1-P3 by replacing min with max. The use of the obvious relation max x A x = (min x A ( x)), by replacing the costs a ij by a ij, works only for digraphs in which, for each circuit C, we have a(c) 0 (in particular, this approach works for digraphs without circuits). If the digraph has circuits, longest path problems are in general NP-hard. 53 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar Most of the exercises for this seminar are very easy, their purpose is solely for practicing the terminology introduced in the lecture. 1 Let us consider three graphs, G 1, G 2, G 3, such that G 1 = G2 and G 2 = G3. True or false: G 1 = G3? You must provide a proof for the answer true or a counterexample for the answer no. 2 Are the two graphs below isomorphic? (justify your answer) 54 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 3 Prove that if G is a connected graph having exactly one circuit then G = E(G). 4 Find the stability number, α(g), of the graph G below (justify your answer). 5 Let G be a connected graph with G > 1 and without leaf vertices. Prove that E(G) G. 55 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 6 Let G = (V, E) be a connected graph with G 2. Prove that there is at least one vertex v 0 V that is not a cut vertex. 7 There are graphs having just one spanning tree? There are graphs having exactly 2 spanning trees? (justify your answers) 8 Draw the graph L(L(G)), where G is: 56 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 9 If G is the graph below, is its line graph, L(G), a Hamiltonian graph? (justify your answer) 10 Find the chromatic number, χ(g), of the complement of the above graph. 11 Find the vertex conectivity number, k(g), of the graph G in the exercise 9. 57 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 12 Is the graph below self-complementary? (justify your answer) 13 Has the above graph two spanning trees without common edges? (justify your answer) 14 Find the number of spanning trees of the complement of the graph in exercise 12. (justify your answer) 58 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 15 Find the maximum cardinality of a stable set in the graph K 2 G, where G is the graph depicted in the exercise 12. 16 If G is the graph below, is L(G) a Hamiltonian graph? (justify your answer) 17 For the above graph, G, find the chromatic number χ(g). (justify your answer) 59 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 18 Is the graph from exercise 16 isomorphic with its complement? (justify your answer) 19 Find the vertex connectivity number of the graph in exercise 16. (justify your answer) 20 Find the diameter of the graph in exercise 16. (justify your answer) 21 Find the chromatic index of the graph in exercise 16. (justify your answer) 60 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 22 Prove that if G is the line graph of a graph H (G = L(H)) then G is a K 1,3 -free graph. 23 Draw the graph P 4 K 2 and find its chromatic number. (justify your answer) 24 Is it true that α(g) k(g) for the graph G below? (justify your answer) 61 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 25 Let G be a connected graph with the property that the dfs and bfs trees starting from the same root vertex are equal. Can G have citcuits? (justify your answer) 26 Prove that if N G (u) N G (v) = V (G) for all u, v V (G), u v, then G is a complete graph. 27 Prove that if a graph G has exactly 2 odd degree vertices, then there is in G a path between these 2 vertices. 28 Let G = (V, E) be a graph with the property that d G (v) + d G (w) V 1, v, w V, v w. Prove that the diameter of G is not greater than 2. 62 / 63
Exercises for Next week (23 Oct - 28 Oct) Seminar 29 Let G = (V, E) be a connected graph with all vertices of even degree. Prove that the graph G e is connected, e E. 30 Let G = (V, E) be a graph with at least 3 vertices. Prove that G is connected if and only if there are two vertices u, v V (u v) such that the graphs G u and G v are connected. 31 If H = (V (H), E(H) is a graph, we denote by e(h) the size of H (e(h) = E(H) ). Prove that for every graph G with at least 3 vertices we have e(g) = v V (G) e(g v) V (G) 2. 63 / 63