GRAPH ALGORITHMS Week 6 (7 Nov- 12 Nov 2016) C. Croitoru [email protected] FII November 5, 2016 1 / 42 OUTLINE Trees I I I I Basics Generating all spanning trees Counting spanning trees: Kirchhoff’s Theorem Minimum Spanning Tree (MST) Problem F F F F I MST General Method Prim’s Algorithm Kruskal’s Algorithm Tarjan’s Union-Find Matchings Exercises for the next week Seminar (14 Nov - 19 Nov 2016) Homework 1 – Presentation 2 / 42 Trees Number of spanning trees: |TG | Let G = (V , E ) be a multi-graph with V = {1, 2, . . . , n}, and adjacency matrix A = (aij )n×n (aij = multiplicity of edge ij if ij ∈ E , 0 otherwise). Let dG (1) 0 0 0 0 dG (2) 0 0 D = diag (dG (1), dG (2), ..., dG (n)) = .. 0 . 0 0 0 0 0 dG (n) The Laplacian matrix of G is defined as: L[G ] = D − A. Note that the sum of the entries in L[G ] on every row and every column is 0. We denote by L[G ]ij the minor of the matrix L[G ] obtained by removing the i th row and j th column. 3 / 42 Trees Number of spanning trees: |TG | Matrix-tree Theorem (Kirchhoff-Trent). Let G be a (multi)graph with vertex set {1, . . . , n} and Laplacian matrix L[G ]. Then, the number of spanning trees of G is: |TG | = det(L[G ]ii ), ∀i ∈ {1, . . . , n}. Proof. Omitted. Corollary (Cayley’s formula). Indeed, n−1 −1 L[Kn ] = .. . −1 −1 .. . .. . ... ... .. . .. . −1 |TKn | = nn−2 n − 1 −1 .. . −1 , det(L[Kn ]11 ) = .. . −1 −1 n−1 −1 .. . .. . ... ... .. . .. . −1 −1 .. . −1 n−1 = nn−2 . 4 / 42 Trees Minimum Spanning Tree Problem MST Problem Given G = (V , E ) a graph and c : E → R (c(e) – the cost of the edge e), find T ∗ ∈ TG such that c(T ∗ ) = min{c(T ) | T ∈ TG }, where c(T ) = P e∈E (T ) c(e). 5 / 42 Trees MST – General Method G = ({1, 2, . . . , n}, E ) Start with the family T 0 = (T10 , T20 , . . . , Tn0 ) of disjoint trees, Ti0 = ({i}, ∅), i = 1, n. k ) of In each step k (k = 0, n − 2), from the family T k = (T1k , T2k , . . . , Tn−k Sn−k Sn−k k k n − k disjoint trees such that i=1 V (Ti ) = V and i=1 E (Ti ) ⊆ E , construct T k+1 as follows: ∇ Choose Tsk ∈ T k . ∇ Find a minimum cost edge e ∗ from the set of edges of G with an extremity in V (Tsk ) and the other in V − V (Tsk ). Let e ∗ = vs vj ∗ , with vj ∗ ∈ V (Tjk∗ ). ∇ T k+1 = (T k \ {Tsk , Tjk∗ }) ∪ T , where T is the tree obtained from Tsk and Tjk∗ by adding the edge e ∗ . If, at some step, there is no edge with an extremity in V (Tsk ) and the other in V − V (Tsk ), it follows that G is not connected and there is no MST in G . 6 / 42 Trees MST – General Method The above construction is suggested in the figure below: vs Tsk v j* Tk j* 7 / 42 Trees MST – General Method The family T n−1 has just one tree T1n−1 . Theorem. If G = (V , E ) is a connected graph with V = {1, 2, . . . , n}, then T1n−1 constructed by the above algorithm is an MST of G Proof. We prove that ∀k ∈ {0, 1, .., n − 1} ∃T ∗ MST in G , such that n−k E (T k ) = ∪i=1 E (Tik ) ⊆ E (T ∗ ). In particular, for k = n − 1, E (T n−1 ) = E (T1n−1 ) ⊆ E (T ∗ ) implies T1n−1 = T ∗ (inclusion holds with equality) and the theorem is proved. For k = 0, we have E (T 0 ) = ∅ and, since G is connected, there is an MST T ∗ ; therefore the (blue) property holds. If the property holds for 0 ≤ k ≤ n − 2, then there is an MST T ∗ such that E (T k ) ⊆ E (T ∗ ). By construction, E (T k+1 ) = E (T K ) ∪ {e ∗ }. If e ∗ ∈ E (T ∗ ), then, E (T k+1 ) ⊆ E (T ∗ ) and the property holds for k + 1. 8 / 42 Trees MST – General Method (Proof cont.) Suppose that e ∗ ∈ / E (T ∗ ). Then, T ∗ + {e ∗ } has exactly one circuit C , ∗ containing e = vs vj ∗ . Since vj ∗ ∈ / V (Tsk ), it follows that there is en edge ∗ e1 6= e in C with an extremity in V (Tsk ) and the other in V \ V (Tsk ). By the choosing of e ∗ we have c(e ∗ ) ≤ c(e1 ) and e1 ∈ E (T ∗ ) \ E (T k ). C vs v j* Tsk T* 9 / 42 Trees MST – General Method (Proof cont.) Let T 1 = (T ∗ + {e ∗ }) − {e1 }. Clearly, T 1 ∈ TG (because is connected and has n − 1 edges). Since e1 ∈ E (T ∗ ) \ E (T k ), we have E (T k+1 ) ⊆ E (T 1 ). Since c(e ∗ ) ≤ c(e1 ), we have c(T 1 ) = c(T ∗ ) + c(e ∗ ) − c(e1 ) ≤ c(T ∗ ). Because T ∗ is an MST, it follows c(T 1 ) = c(T ∗ ), that is T 1 is the MST containing all the edges in E (T k+1 ) and the theorem is proved. Remarks. The above proof remains true for tree-cost functions c : TG → R satisfying: ∀T ∈ TG , ∀e ∈ E (T ), ∀e 0 ∈ / E (T ) c(e 0 ) ≤ c(e) ⇒ c((T + e 0 ) − e) ≤ c(T ). In the general method presented, the way of choosing the tree T k is not detailed. We will discuss two famous different strategies. 10 / 42 Trees MST – Prim’s Algorithm Tks is a tree of maximum order in the family T k . It follows that in each step k > 0 of the general method, T k has a tree, Ts = (Vs , Es ), with k + 1 vertices and n − k − 1 trees with just one vertex. Dijkstra’s implementation: Let α and β be two vectors of size n; the α-entries are vertices (from {1, . . . , n}) and the β-entries are real numbers, with the following meaning: (S) ∀j ∈ V − Vs , β j = c(α[j]j) = min{c(ij) | i ∈ Vs , ij ∈ E } beta(v) v x alfa(v) beta(x)=+infinit w alfa(u) beta(w) beta(u) Ts alfa(w) u 11 / 42 Trees MST – Prim’s Algorithm 1. Vs :← {s}; Es ← ∅; (for some s ∈ V ) for v ∈ V \ {s} do { α[v ] := s; β[v ] := c(sv )}; // if ij ∈ / E then c(ij) = ∞). 2. while Vs 6= V do { find j ∗ ∈ V \ Vs such that β[j ∗ ] = min{β[j] | j ∈ V − Vs } ; Vs ← Vs ∪ {j ∗ }; Es := Es ∪ {α[j ∗ ]j ∗ }; for j ∈ V − Vs do if β[j] > c[j ∗ j] then { β[j] ← c[j ∗ j]; α[j] :← j ∗ } } Note that (S) is satisfied after the initializations in the step 1. In the step 2, the strategy of the general method is respected and also the meaning of (S) is maintained by the test in the for loop. Time complexity: O(n − 1) + O(n − 2) + . . . + O(1) = O(n2 ). This is good for graphs with size O(n2 ). 12 / 42 Trees MST – Kruskal’s Algorithm Tks is one of the two trees in the family T k , connected by an edge of minimum cost over all edges with extremities on different trees of the family. This choice can be done by performing firstly a non-decreasing sorting of the edges by their costs and, after that, parsing the obtained list. If we denote by T the tree Tks , the algorithm can be described as follows. 1. Sort E = (e1 , e2 , . . . , em ) such that: c(e1 ) ≤ c(e2 ) ≤ . . . ≤ c(em ). 1.2 T ← ∅; i ← 1; 2. while i ≤ m do { if hT ∪ {ei }iG has no circuits then T ← T ∪ {ei } ; i ++ } 13 / 42 Trees MST – Kruskal’s Algorithm Step 1 can be done in O(m log m) = O(m log n) time. In order to efficiently implement the test in step 2, it is necessary to represent the sets of vertices of the trees, V (T1k ), V (T2k ), . . . , V (Tnk ) (at each step k of the general method), and to test if the current edge has both extremities in the same set. These sets will be represented using trees (which are not, in general, subtrees of the graph G ). Each such tree has a root which will be used to designate the set of vertices of the graph G stored in the tree. More precisely, we have a function find(v) which finds the set to which the vertex v belongs, that is, returns the root of the tree storing the set to which v belongs. 14 / 42 Trees MST – Kruskal’s Algorithm – Union-Find In the general method it is necessary to make the (disjoint) union of the vertex sets of two trees (in order to obtain T k+1 from T k ). We use a procedure union(v, w) with the following meaning: make the union of the two sets of vertices, to which v and w belong . We can write the step 2 of the algorithm, using these two procedures, as follows: 2. while i ≤ m do { let ei = vw ; if find(v ) 6= find(w ) then { union(v , w ); T ← T ∪ {ei } }; i ++ } The time complexity of the algorithm depends on the implementation of the procedures find and union. 15 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – First Solution The array root[1..n] with entries from V has the meaning root[v ] =root of the tree storing the set to which v belongs. Add to step 1 the initialization (corresponding to the family T 0 ): 1.3 for v ∈ V do root[v ] ← v ; The function find (having time complexity O(1)): function find(v : V ); return rad[v ] The procedure union (having time complexity O(n)): procedure union(v , w : V ); for i ∈ V do if rad[i] = rad[v ] then rad[i] := rad[w ] 16 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – First Solution Time complexity analysis: The are O(m) calls of the function find during the step 2. In the sequence of find-calls, exactly n − 1 union-calls are interleaved (one call for each edge of the final MST). Therefore, the time spent by the algorithm during the step 2 is O(mO(1) + (n − 1)O(n)) = O(n2 ). Hence, the time complexity of the algorithm is O(max(m log n, n2 )). If G has many edges, m = O(n2 ), the Prim’s algorithm is more efficient. 17 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution The array pred[1..n] with entries from V ∪ {0} has the meaning pred[v ] = the vertex before v on the unique path to v from the root of the tree storing the set to which v belongs. pred[v ] = 0 if and only if v is the root of this tree. Add to step 1 the initialization (corresponding to the family T 0 ): 1.3 for v ∈ V do pred[v ] ← 0; The function find runs in time O(h(v )), where h(v ) is the length of the tree-path from v to the root (of the tree that contains v ): function find(v : V ); local variable i : V ; i ← v; while pred[i] > 0 do i ← pred[i]; return i 18 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution The procedure union (having time complexity O(1)) is called only for root vertices: procedure union(root1, root2 : V ); pred[root1] ← root2 The step 2 of the algorithm is changed to support this modifacation: 2. while i ≤ m do { let ei = vw ; x :← find(v ); y ← find(w ); if x 6= y then { union(x, y ); T ← T ∪ {ei } }; i ++ } 19 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution If we execute this form of the Step 2 to the graph G = K1,n−1 with the sorted edge list, E = {12, 13, . . . , 1n}, then the sequence of calls of the two procedures is (F and U abbreviates find and union): F (1), F (2), U(1, 2), F (1), F (3), U(2, 3), . . . , F (1), F (n), U(n − 1, n). F(1) F(2) U(1,2) 2 3 F(1) F(3) U(2,3) F(1) F(4) U(3,4) 1 2 3 n 1 2 3 4 n 1 2 3 4 n 1 2 3 1 n F(1) F(n) U(n-1,n) 4 n-1 n The calls F (i)(i > 1) and U(i, i + 1) i ≥ 1 take O(n) overall time. The sequence of F (1) calls requires O(1) + O(2) + · · · + O(n − 1) = O(n2 ) time. 20 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution Hence this form of the algorithm has time complexity Ω(n2 ) (even if the graph is sparse). The weakness of this implementation is given by the fact that, in the union procedure, we make the root of the new tree that of the tree storing a smaller number of vertices, implying an augmenting of h(v ) – to O(n) – during the algorithm. We can avoid this by keeping in the root of each tree the cardinality of the set stored. More precisely, the meaning of pred[v ], when v is a root, is : pred[v ] < 0 ⇔ v is the root of a tree storing a set with −pred[v ] vertices. 21 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution The initialization step is 1.3 for v ∈ V do pred[v ] ← −1; The union take also O(1) time to maintain the new meaning: procedure union(root1, root2 : V ); t ← pred[root1] + pred[root2]; if −pred[root1]geq − pred[root2] then { pred[root2] ← root1; pred[root1] ← t } else { pred[root1] ← root2; pred[root2] ← t } Fact. With this implementation of the procedures find and union the algorithm has as invariant:(∗) ∀v ∈ V − pred[find(v )] ≥ 2h(v ) . (In words: the number of vertices stored in the tree to which v belongs is at least 2 to the power ”distance of v to the root”.) 22 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution Proof of the Fact. After the initialization step 1.3, we have ∀v ∈ V h(v ) = 0, find(v ) = v , and −pred[v ] = 1, therefore (∗) holds with equality. Suppose that (∗) holds before a while iteration in the step 2. In this while iteration union is not called. The array pred is not updated, so (∗) remains fulfilled after this iteration. In this while iteration we have a call of the procedure union. Let union(x, y ) be this call, and suppose that in the union procedure the assignment pred[y ] := x is executed. This means that before this iteration we have −pred[x] ≥ −pred[y ] The vertices v for which h(v ) changes in this iteration are those for which, before the iteration, find(v ) = y , and for which we had −pred[y ] ≥ 2h(v ) . After the while iteration, we have h0 (v ) = h(v ) + 1 and 0 find 0 (v ) = x. Hence we must verify if −pred 0 [x] ≥ 2h (v ) . Indeed, −pred 0 [x] = −pred[x] − pred[y ] ≥ 2· (−pred[y ]) ≥ 2· 2h(v ) = 2h(v )+1 = 0 2h (v ) . It follows that (∗) is an invariant of the algorithm. 23 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Second Solution By taking the logarithm in (∗), we obtain h(v ) ≤ log(−pred[find[v ]]) ≤ log n, ∀v ∈ V . Therefore, the time complexity of the step 2 is O(n − 1 + 2m log n) = O(m log n). Hence this second implementation of the procedures union--find gives an O(m log n) time complexity of the Kruskal’s algorithm. 24 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Third Solution The time complexity of the step 2 in the above solution is due to the sequence of find calls. Tarjan (1976) observed that a call with h(v ) > 1 can – without changing the O(h(v ) time – ”collapse” the tree path from v to the root, making h(x) = 1 for all vertices x on that path. In this way, the future find calls for these vertices will take small time. More precisely, the function find is: function find(v : V ); local variable i, j, k; i ← v; while pred[i] > 0 do i ← pred[i]; j ← v; while pred[j] > 0 do { k ← pred[j]; pred[j] ← i; j ← k; } return i 25 / 42 Trees MST – Kruskal’s Algorithm – Union-Find – Third Solution If A : NxN → N is the Ackermann function n+1 A(m − 1, 1) A(m, n) = A(m − 1, A(m, n − 1)) given by : if m = 0 if m > 0 and n = 0 if m > 0 and n > 0 (1) and if we denote ∀m ≥ n > 0, α(m, n) = min{z | A z, 4dm/ne ≥ log n, z ≥ 1} we have: The time complexity of step 2, using union from the second solution and the above find, is O(m· α(m, n)). Note that α(m, n) is an extremely slow increasing function, and for practical values of n α(m, n) ≤ 3 and the third solution is practically a linear implementation (O(m)) of Kruskal’s algorithm. 26 / 42 Matchings Maximum Matching – Minimum edge-cover Let G = (V , E ) be a (multi)graph. If A ⊆ E and v ∈ V , we denote dA (v ) = |{e | e ∈ A, e incident with v }|, that is, the degree of v in the spanning subgraph < A >G . Definition. A matching (independent set of edges) in G is any set of edges M ⊆ E such that dM (v ) ≤ 1, ∀v ∈ V . The family of all matchings in the graph G is denoted MG : MG = {M | M ⊆ E , M matching in G }. Note that MG satisfies: i) ∅ ∈ MG . ii) M ∈ MG , M 0 ⊆ M ⇒ M 0 ∈ MG . 27 / 42 Matchings Maximum Matching – Minimum edge-cover Let M ∈ MG be a matching. A vertex v ∈ V with dM (v ) = 1 is called saturated by M, and the set of all vertices of G saturated by M is denoted S(M). Clearly, S(M) = ∪e∈M e, and |S(M)| = 2 · |M|. A vertex v ∈ V with dM (v ) = 0 is called exposed (with respect) to M, and the set of all vertices of G exposed to M is denoted E(M). Clearly, E (M) = V − S(M), and |E(M)| = |V | − 2 · |M|. Maximum Matching Problem: P1 Given a graph G = (V , E ), find M ∗ ∈ MG such that |M ∗ | = max{|M| | M ∈ MG }. (We denote by ν(G ) = max{|M| | M ∈ MG }). 28 / 42 Matchings Maximum Matching – Minimum edge-cover The maximum matching problem is closely related to the minimum edge-cover problem. Definition. An edge-cover in G is any set of edges F ⊆ E such that dF (v ) ≥ 1 ∀v ∈ V . The family of all edge-covers in the graph G is denoted FG : FG = {F | F ⊆ E , F edge-cover in G }. Note that FG 6= ∅ ⇔ G has no isolated vertices (then, E is an edge-cover). Minimum Edge-cover Problem: P2 Given a graph G = (V , E ), find F ∗ ∈ FG such that |F ∗ | = min{|F | | F ∈ FG }. 29 / 42 Matchings Maximum Matching – Minimum edge-cover Theorem 1. (Norman-Rabin 1959) Let G = (V , E ) be a graph of order n, without isolated vertices. If M ∗ is a maximum cardinality matching in G and F ∗ is a minimum cardinality edge-cover, then |M ∗ | + |F ∗ | = n. Proof. a) Let M ∗ be maximum cardinality matching in G and consider the following algorithm: F ← M ∗; for ∀v ∈ E(M ∗ ) do { find v 0 ∈ S(M ∗ ) such that vv 0 ∈ E ; F ← F ∪ {vv 0 } } Note that ∀v ∈ E (M ∗ ), since G has no isolated vertices, there is an edge incident with v , and – since M ∗ is maximal w.r.t. inclusion – this edge has the other extremity in S(M ∗ ). The set F of edges constructed is an edge-cover and |F | = |M ∗ | + |E (M ∗ )| = |M ∗ | + n − 2|M ∗ | = n − |M ∗ |. Hence |F ∗ | ≤ |F | = n − |M ∗ |. 30 / 42 Matchings Maximum Matching – Minimum edge-cover b) Let Fie F ∗ be a minimum edge-cover and consider the following algorithm M ← F∗ while ∃v ∈ V : dM (v ) > 1 do { find e ∈ M incident with v ; M ←M −e } Clearly, the algorithm constructs a matching M in G . If the edge e incident with v (removed from M in a while iteration) is e = vv 0 , then dM (v 0 ) = 1 and in the next iteration we will have dM (v 0 ) = 0, hence at each while iteration an exposed vertex w.r.t. the final matching M is created (if there would be another edge e 0 in the current set M incident with v 0 , then e 0 ∈ F ∗ and F ∗ − e 0 would be an edge-cover, contradicting the choice of F ∗ ). Hence, if M is the mathcing constructed by the algorithm, we have: |F ∗ | − |M| = |E (M)| = n − 2|M|, that is, |F ∗ | = n − |M| ≥ n − |M ∗ |. From a) and b) the theorem follows. Note that we have proved that the two problems P1 and P2 are polynomially equivalent since the matching M and the edge-cover F constructed are optimal solutions, respectively. 31 / 42 Exercises for Next week (14 Nov - 19 Nov 2016) Seminar The following exercises will be discussed at the groups that have the seminar scheduled Monday. All other groups will discuss, in the week 14 Nov - 19 Nov 2016, the exercises from the homework 1. In the week 9 28 nov - 3 dec, the Monday groups will discuss the homework 1 and the other groups the following exercises. 32 / 42 Exercises for Next week (14 Nov - 19 Nov 2016 / 28 Nov - 3 Dec) Seminar 1 Let G = (V , E ) be a connected graph and c : E → R a weight function on its edges. A subset A ⊆ E is called cut if exists a bipartion (S, T ) of V such that A = {uv ∈ E : u ∈ S, v ∈ T } (G \ A is no more connected). a) If in every cut it exists only one edge of minimum weight, then G contains only one minimum weight spanning tree. b) Show that, if c is an injective function, then G contains only one minimum weight spanning tree. c) Are true the converse assertions? 2 Prove that any tree with maximum degree t > 0 has at least t pendant nodes. 33 / 42 Exercises for Next week (14 Nov - 19 Nov 2016 / 28 Nov - 3 Dec) Seminar 3 Consider an ordering E = {e1 , e2 , . . . , em } of the edges of a connected graph G = (V , E ) of order n. For every subset A ⊆ E we define x A ∈ GF m the m-dimensional characteristic vector: xiA = 1 ⇔ ei ∈ A. GF m is the m-dimensional 0 - 1 vector space over Z2 . a) Show that the subset of the characteristic vectors corresponding to all cuts in G completed with zero vector is a subspace X of GF m . b) Show that the subset of the characteristic vectors corresponding to all the circuits in G spans a subspace U of GF m which is orthogonal on X. c) Prove that dim(X ) ≥ n − 1. d) Show that dim(U) ≥ m − n + 1. e) Finally, prove that the above inequalities are in fact equalities. 34 / 42 Exercises for Next week (14 Nov - 19 Nov 2016 / 28 Nov - 3 Dec) Seminar 4 Let T = (V , E ) a rooted tree; we denote by r its root and by parent(v ) the direct ancestor of any v 6= r . A matching M of T is called proper if any unmatched vertex v 6= r has a brother w such that w parent(v ) ∈ M. a) Show that any proper matching is a maximum cardinality matching. b) Describe an algorithm that determine in O(n) a proper matching in a given tree with n vertices . 5 Let G = (S, T ; E ) a bipartite graph. Use Hall’s theorem to prove that, for every 0 ≤ k ≤ |S|, G has a matching of cardinality at least |S| − k if and only if |NG (A)| ≥ |A| − k, ∀A ⊆ S. 35 / 42 Exercises for Next week (14 Nov - 19 Nov 2016 / 28 Nov - 3 Dec) Seminar 6 Let G = (V , E ) be a connected graph of order n and TG the family of all its spanning trees. Define H = (TG , E (H)) where T1 T2 ∈ E (H) ⇐⇒ |E (T1 )∆E (T2 )| = 2. a) Show that H is connected and its diameter is at most n − 1. b) For every weight on the edges of G , the set of minimum weight spanning trees induces a connected subgraph in H. 7 Let G = (V , E ) a graph. A subset A ⊆ V is called m-independent if G has a matching M which saturates all the vertices from A. Show that, for any two m-independent sets A and B with |A| < |B|, we can find a vertex b ∈ B \ A such that A ∪ {b} is also m-independent (⇒ all maximal m-independent sets have the same cardinality). 36 / 42 Homework 1 – Presentation Problema 1. Fie A un algoritm cu timp de lucru polinomial, care, primind la intrare un graf oarecare G , returnează o mulţime stabilă SA (G ) a lui G cu proprietatea α(G ) − |SA (G )| ≤ k, pentru o constantă k ∈ N. Arătaţi că A poate fi folosit pentru a determina ı̂n timp polinomial o mulţime stabilă de cardinal maxim ı̂ntr-un graf oarecare dat. (2 puncte) 37 / 42 Homework 1 – Presentation Problema 2. Să se determine graful G conex de ordin n (n ≥ 3) cu proprietatea G ' L(G ). (2 puncte) 38 / 42 Homework 1 – Presentation Problema 3. Fie G = (V , E ) un digraf cu V = {1, . . . , n}, |E | = m şi a : E → R>0 o funcţie de cost pe arcele sale. Dacă Pij este un drum de la i la j ı̂n G , costul minim al unui arc al acestui drum se numeşte gâtuirea sa. Fie s ∈ V . Dorim să aflăm câte un drum de gâtuire maximă de la s la i, ∀i ∈ V . a) Scrieţi sistemul de ecuaţii (B) corespunzător pentru această problemă (vezi notele de curs, week3.pdf, slide-ul 32). b) Adaptaţi algoritmul lui Dijkstra pentru rezolvarea problemei (vezi notele de curs, week4.pdf, slide-ul 10) şi demonstraţi corectitudinea algoritmului propus. (1+(1+2)=4 puncte) 39 / 42 Homework 1 – Presentation Problema 4. a) Considerăm G = (V (G ), E (G )) un graf şi următoarea funcţie: function split(G ) if toate vârfurile lui G au grad par then return (V (G ), ∅) else { fie a ∈ V un vârf de grad impar; S ← NG (a); G ← G − a; for ∀u, v ∈ S, u 6= v do if uv ∈ E (G ) then E (G )← E (G ) - uv else E (G )←E (G )∪uv ; (W1 , W2 ) ← split(G ); if |W1 ∩ S| ≡ 0( mod 2) then { V1 ← W1 ∪ {a}; V2 ← W2 } else { V2 ← W2 ∪ {a}; V1 ← W1 }; return (V1 , V2 ) } Demonstraţi că perechea de mulţimi returnate satisface următoarele proprietăţi: V1 ∩ V2 = ∅, V1 ∪ V2 = V (G ) iar grafurile [V1 ]G , [V2 ]G au toate vârfurile de grad par (dacă una dintre cele două mulţimi e vidă, atunci condiţia de paritate se pune doar pe subgraful indus de cealaltă). 40 / 42 Homework 1 – Presentation Problema 4. (continuare) b) Fie o reţea socială reprezentată de un graf G . Sociologii au descoperit următoarea proprietate de influenţare a opiniilor membrilor acestei reţele: ∀v ∈ V (G ), dacă persoana v este influenţată, atunci v ı̂şi schimbă opinia ov pe care o are ı̂n ¬ ov şi, ı̂n plus, orice vecin w al lui v ı̂n G (w ∈ NG (v )) ı̂si schimbă şi el opinia (din ow ı̂n ¬ ow ). De asemenea, sociologii au obsevat că dacă toţi membrii reţelei au aceeaşi opinie o, atunci există o mulţime de membri S cu proprietatea că influenţând exact o dată, pe rând, fiecare membru al lui S (nu are importanţă ordinea) se ajunge la situaţia că toţi membrii reţelei au opinia ¬ o. b1) Notăm cu m(v , X ) numărul muchiilor de la vârful v ∈ V la mulţimea de vârfuri X ⊆ V . Caracterizaţi mulţimea S (descoperită de sociologi) precizând proprietătile numerelor m(v , S) pentru v ∈ V . b2) Descrieţi un algoritm care să returneze mulţimea S pentru un graf dat, folosind funcţia de la a) (demonstraţi corectitudinea). (2+2+2 puncte) 41 / 42 Homework 1 – Presentation Soluţiile se primesc luni 14 noiembrie ı̂ntre orele 12 şi 14, la cabinetul C-402 Precizări Este ı̂ncurajată asocierea ı̂n echipe formate din 2 studenţi care să rezolve ı̂n comun tema. Depistarea unor soluţii copiate ı̂ntre echipe diferite conduce la anularea punctajelor tuturor acestor echipe. Nu e nevoie să se rescrie enunţul problemelor. Nu uitaţi să vă treceţi numele şi grupa din care faceţi parte, la ı̂nceputul lucrarii. Este ı̂ncurajată redactarea latex a soluţiilor. Nu se primesc soluţii prin e-mail. Economisiţi hârtia!! (temele ce depăşesc 8 pagini nu sunt binevenite) 42 / 42
© Copyright 2026 Paperzz