Procedure greedy var G GRAPH var newclr SET

  • Slides: 24
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} 2 Data Structures, CS, TAU - 2. 2

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

עידון האלגוריתם להשגת תוכנית . (3 מציאה אם צומת שכן )שורה : דוגמא (if v is not adjacent to any vertex in newclr then begin) (3. 1) found : = false; (3. 2) for each vertex W in newclr do (3. 3) (3. 4) if there is an edge between V and W in G then found : = true; (3. 5) if found = false then begin (4). (5) 3 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} 10 Data Structures, CS, TAU - 2. 9

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

( חישוב עצרת )רקורסיבי : דוגמא 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) 11 Data Structures, CS, TAU - 2. 10

 ציור מדדי סיבוכיות cg(n) f(n) cg(n) n 0 c 2 g(n) c 1

ציור מדדי סיבוכיות cg(n) f(n) cg(n) n 0 c 2 g(n) c 1 g(n) c 2 g(n) f(n) c 1 g(n) n 0 14 Data Structures, CS, TAU - 2. 13

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

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

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

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

a 3 דוגמא b f(n) : 3 מקרה : גדול n עבור 24 Data

a 3 דוגמא b f(n) : 3 מקרה : גדול n עבור 24 Data Structures, CS, TAU - 2. 23