130 likes | 230 Views
". ¥. d[1]:=0;. i>1: d[. i]:=. ;. Æ. S:=. ;. Repeat. Î. È. u:=arg. min. {d[. i]|. i. NS}, S:=S. {u}. ". Î. i. NS: d[. i]:=. min. {d[. i],d[u]+c[. u,i]}. until. S=N. 11,1. 10,2. 5,1. 5. 4. 16. 2. 13. 5. 15. 8. 11. 0,1. 17,1. 26,4. 8. 7. 17. 1.
E N D
" ¥ d[1]:=0; i>1: d[ i]:= ; Æ S:= ; Repeat Î È u:=arg min {d[ i]| i N\S}, S:=S {u} " Î i N\S: d[ i]:= min {d[ i],d[u]+c[ u,i]} until S=N 11,1 10,2 5,1 5 4 16 2 13 5 15 8 11 0,1 17,1 26,4 8 7 17 1 5 9 7 18,4 3 7 13,4 14 -5 3 12 3 M,0 8 9 25,4 6 14 Shortest path with negative arc-costs allowed. Dijkstra? No! Better now Bellman-Ford:
Search(s: node); input A(i)’s of a (di)graph (N,A) [1];node-labels pr(i)N; integer k(=0); output for j in component of s an s-j path begin iN: pr(i):=0; pr(s):=s; LIST:=<s>; whileLIST do remove the first node from LIST, say i; k:=k+1; /* just counting the number of list-removals/scannings for all(i,j)A(i) with pr(j)=0do pr(j):=i; put j on LIST; end; iN: pr(i)=0; compNr:=0; for all sN with pr(s)=0 do compNr:=compNr+1,Search(s) ; [1] if (N,A) is undirected then for any edge (i,j): (i,j) A(i) and (j,i) A(j)
reNumbering input A(i)’s of acyclic digraph (N,A);node-labels nr(i), indgr(i)Z; integer k(=0); output topological numbering s.t. nr(i)<nr(j) for (i,j) begin iN: nr(i):=0; indgr(i):=|inA(I)|; LIST:=<s: with indegree(s)=0>; whileLIST do remove a node from LIST, say i; k:=k+1; nr(i):=k; /* here is a purpose withNr(k):=i for all(i,j)A(i)do indgr(j):=indgr(j)-1; if indgr(j)=0 then put j on LIST; end; DPshortestpaths s:=withNr[1]; d(s):=0; for k:=2 to ndo i:=withNr[k], (j,i) inA(i) : if d(j)+c(j,i)< d(i)thend(i):=d(j)+c(j,i) ; k:=0; reNumbering; ifk=nthen write('graph is acyclic');
Search(s: node) input A(i)’s of a (di)graph G=(N,A);labels pr(i)N{0}; output for j in component of s an s-j path begin iN: pr(i):=0; pr(s):=s; LIST:=<s>; whileLIST do remove a node from LIST, say i; for all(i,j)A(i) with pr(j)=0do pr(j):=i; put j on LIST; end;
BreadthFirst(s: node); /* scan first nodes first: fifo list management input A(i)’s of a (di)graph G=(N,A); labels pr(i)N{0}, d(i) Z0; output for j in component of s an arc-minimals-j path with d(j) arcs begin iN: pr(i):=0, d(i):=; pr(s):=s; LIST:=<s>; d(s):=0; whileLIST do remove the first node from LIST, say i; for all(i,j)A(i) with d(j)=do pr(j):=i, d(j):=d(i)+1; put jlast on LIST; end;
Dijkstra(s: node); /* scan 'best' nodes first input A(i)’s of a (di)graph G=(N,A);labels pr(i)N{0}, d(i) Z0; output for j in component of s a minimal-costs-j path of cost d(j) begin iN: pr(i):=0, d(i):=; pr(s):=s; LIST:=<s>; d(s):=0; whileLIST do remove the best node from LIST, say i with minimal d(i); for all(i,j)A(i) with d(j)>d(i)+c(i,j)do pr(j):=i, d(j):=d(i)+c(i,j) put j on LIST (if not yet in) end;
Complexiteitstheorie: YES\NO recognition problems TSP>=k Instance: netwerk (N,E,c) met c:EZ+ ; getal k Question:Is er een tour met lengte < k? • Stel • algoritme A lost op: yes-no versies TSP<=k in complexiteit tA(n) • er is algoritme B voor TSPoptin complexiteit tB(n)= log(nC) · tA(n) • algoritme B*/ past bisectie toe met A • U:=nC; */ C=max {cij| (i,j) in E} • L:=0; */U is boven- en L is ondergrens op z* • Repeat • k:=(U+L)/2; • if algoritme A op TSP<=k returns YES then U:=k • else L:=k ; • /* opnieuw geldt: U is boven- en L is ondergrens op z* • en het interval [L,U] is gehalveerd ! • Until |U-L|< 1;
Voorbeelden YES\NO recognition problems Graph-Connectedness Instance: graph G=(V, E) Question:Is G connected? Satisfiability (informeel) Instance:Boolean expression Question: Kan deze expressie de waarde ‘true’ aannemen (is it satisfiable) Langste Pad Instance: netwerk (N,E,c) met c:EZ+ ; s,t N; getal k Question:Is er een s-t pad P met lengte > k? LP Instance: (c,A,b) resp. n-vector, mxn matrix en m-vector;getal k Question:Is max{cx| xRn met Ax<=b} > k? ILP Zn Graph-Isomorfisme (informeel) Instance: graphs G1=(V1, E1) en G2=(V1, E1) Question:Zijn G1 en G2 hetzelfde?
Graph-Connectedness Instance: graph G=(V, E) Question:Is G connected? MST Instance: graph G=(V, E,c); number k; Question:Is there a tree T spanning V of total weight <=k? Steiner tree Instance: graph G=(V, E,c); subset of nodes K; number k; Question:Is there a tree spanning K of length <=k? UFL Instance: clients i=1,…,m with unit demand, facilities j=1,…n with one-time fixed charge opening costs fj; costs cij for delivering the unitof i from j; number k; Question: Can we satisfy all demandsat a total cost <=k?
DEFINITIONS thetime complexity(function)tAof algorithm A forproblemP is tA(n)= max {#operaties of A on instance I | over IP with |I|=n}P is polynomial solvable - notation P- if algorithm A with tA(n)of O(nq) for someqZ+P is non-deterministicallypolynomial solvable-notation P- if allIP with answer YEShave a ‘-certificate’, checkable in polytime EXAMPLES (if P is polynomial solvable then P is in ) TSP: Instance:network (N,E,c) with number kR Question: tour oflength <=k? a -certificate is a set of n edges; polytime checkable: tour of length <= k coTSP ?! Instance:netwerk (N,E,c) with number kR Question:Are all tours of length > k? no -certificate?!
5 4 16 2 13 5 15 8 11 8 7 10 1 5 9 7 3 7 14 2 3 12 3 8 9 6 14 TSP>=k Instance: netwerk (N,E,c) met c:EZ+ ; getal k Question:Is er een tour met lengte < k? TSP Instance: netwerk (N,E,c) met c:EZ+ ; getal k Question:Is er een tour met lengte < k? ShortestPath Instance: netwerk (N,E,c) met c:EZ+ ; s,t N ; getal k Question:Is er een s-t path van lengte < k?
Transformatie-afbeelding van ITSP naar I':=(I)Kortste Pad - 1) Neem (N',E')=(N,E) met extra kopie n' van n ( dezelfde kanten als n); 2) c' verlaagt alle kantkosten met M 3) s=n en t=n'' 4 +c -M -M 2 ij +16 4) k'=-nM+k 9'' 7 9 1 5 -M +3 Kortste 9 - 9'' pad bevat n kanten - als mogelijk - 3 8 tourlengte voor lengte - nM + 6 I heeft TSP tour <= 63 desda I' heeft kortste 9-9'' pad <= -9M+63
algoritme in O(|I|qr) time voor I in P: doe eerst AP/* geeft I' in SAT van size O(| |r) doe dan A alg. A O(| |q) yes/no P(I)SAT AP O(| |r) IP probleem P1 is reduceerbaar tot P2 als afbeelding :P1 P2zo dat: I yes-instantie van P1 (I) yes-instantie van P2 als ook: polytime-algoritme, zeg A,met inputs IP1 en outputs (I)P2 dan P1 is 'polynomial‑reducable' tot P2 , notatie: P1 P2 TheoremCook (1971): SAT is -compleet, i.e.: each problemP of is polynomial reducable to SAT Corollary If SAT can be solved by some polynomial algorithm A, then all problems P are polynomial solvable (=)Proof: