Procedure greedy var G GRAPH var newclr SET

  • Slides: 23
Download presentation
 שפה - כתיבת הפתרון בפסאודו Procedure greedy (var G: GRAPH; var newclr: SET(

שפה - כתיבת הפתרון בפסאודו Procedure greedy (var G: GRAPH; var newclr: SET( }greedy assigns to newclr a set of vertices that may be given the same color{ begin (1)newclr=: (2)for each uncolored vertex v of G do (3)if v is not adjacent to any vertex in newclr then begin (4)mark v colored (5)add v to newclr end; {greedy{ Data Structures, CS, TAU - 2. 2

 עידון האלגוריתם להשגת תוכנית מציאה אם צומת שכן : דוגמא . (3 )שורה

עידון האלגוריתם להשגת תוכנית מציאה אם צומת שכן : דוגמא . (3 )שורה (3. 1) found : = false; (3. 2) for each vertex W in newclr do (3. 3) if there is an edge between V and W in G then (3. 4) found : = true; (3. 5) if found = false then begin. . (5 ). . . ( 4). Data Structures, CS, TAU - 2. 1

: דוגמא Procedure bubble var I, j, temp : integer begin (1)for I :

: דוגמא Procedure bubble var I, j, temp : integer begin (1)for I : = 1 to n-1 do (2)for j : = n downto I+1 do (3)if A[j-1] > A[j] then begin } swap A[j-1] and A[j{[ (4)temp : = A[j-1; [ (5)A[j-1] : = A[j; [ (6)A[j] : = temp; end; {bubble{ Data Structures, CS, TAU - 2. 9

( חישוב עצרת )רקורסיבי : דוגמא Function fact (n: integer( begin (1)if n<1 then

( חישוב עצרת )רקורסיבי : דוגמא Function fact (n: integer( begin (1)if n<1 then (2)fact : = 1 else (3)fact : = n*fact(n-1( end; {fact{ d T(n = ( c + T(n-1( T(n) = 2 c+T(n-2( T(n) = 3 c+T(n-3(. . T(n) = c(n-1)+T(1)=c(n-1)+d=O(n( Data Structures, CS, TAU - 2. 10

cg(n( f(n( cg(n( n 0 cg(n( c 1 g(n( c 2 g(n( f(n( c

cg(n( f(n( cg(n( n 0 cg(n( c 1 g(n( c 2 g(n( f(n( c 1 g(n( n 0 Data Structures, CS, TAU - 2. 13

a b : דוגמאות f(n( (1 Data Structures, CS, TAU - 2. 21

a b : דוגמאות f(n( (1 Data Structures, CS, TAU - 2. 21

a b : דוגמאות f(n( (2 2 מקרה Data Structures, CS, TAU - 2.

a b : דוגמאות f(n( (2 2 מקרה Data Structures, CS, TAU - 2. 22