Shortest path with negative arc-costs allowed. Dijkstra? " i>1: d[ i]:= ¥ ; S:= Æ Repeat u:=arg min {d[ i]| i Î N\S}, S:=S È {u} N\S: d[ i],d[u]+c[ u,i]} until S=N 1 2 3 4 5 6 7 8 9 14 13 16 12 -5 0,1 5,1 13,4 11,1 10,2 17,1 M,0 18,4 26,4 11 25,4 17 15 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>; while LIST 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)=0 do 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)
output topological numbering s.t. nr(i)<nr(j) for (i,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>; while LIST 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 n do i:=withNr[k], (j,i) inA(i) : if d(j)+c(j,i) < d(i) then d(i):= d(j)+c(j,i) ; k:=0; reNumbering; if k=n then 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>; while LIST do remove a node from LIST, say i; for all (i,j)A(i) with pr(j)=0 do 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-minimal s-j path with d(j) arcs begin iN: pr(i):=0, d(i):= ; pr(s):=s; LIST:=<s>; d(s):=0; while LIST 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 j last 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-cost s-j path of cost d(j) begin iN: pr(i):=0, d(i):= ; pr(s):=s; LIST:=<s>; d(s):=0; while LIST 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 TSPopt in 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? ILP LP Instance: (c,A,b) resp. n-vector, mxn matrix en m-vector; getal k Question: Is max{cx| xRn met Ax<=b} > k? 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 unit of i from j; number k; Question: Can we satisfy all demands at a total cost <=k?
DEFINITIONS the time complexity (function) tA of algorithm A for problem P 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 some qZ+ P is non-deterministically polynomial solvable -notation P- if all I P with answer YES have 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 of length <=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?!
Instance: netwerk (N,E,c) met c:EZ+ ; getal 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? TSP>=k Instance: netwerk (N,E,c) met c:EZ+ ; getal k Question: Is er een tour met lengte < k? 1 2 3 4 5 6 7 8 9 14 13 16 12 11 10 15
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 -M +c -M 2 ij 4) k'=-nM+k +16 9'' 7 9 1 5 Kortste 9 - 9'' pad bevat n -M +3 kanten - als mogelijk - 3 8 voor lengte - nM + tourlengte 6 I heeft TSP tour <= 63 desda I' heeft kortste 9-9'' pad <= -9M+63
probleem P1 is reduceerbaar tot P2 als afbeelding :P1 P2 zo 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 Theorem Cook (1971): SAT is -compleet, i.e.: each problem P 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: IP P(I)SAT AP O(| |r) alg. A O(| |q) yes/no algoritme in O(|I|qr) time voor I in P: doe eerst AP /* geeft I' in SAT van size O(| |r) doe dan A