BFS,DFS Topological Sort

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Comp 122, Fall 2004.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs-Part II Depth First Search (DFS)
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Data Structures, Algorithms & Complexity
Suggested Solutions to Part of Homework 1
Graph Algorithms BFS, DFS, Dijkstra’s.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph.
Graph Representation, DFS and BFS
Binhai Zhu Computer Science Department, Montana State University
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
Breadth-First Search The Breadth-first search algorithm
Algorithms and Data Structures Lecture XII
Applications of DFS Topological sort (for directed acyclic graph)
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Algorithms and Data Structures Lecture XI
Decrease and Conquer Decrease and conquer technique Insertion sort
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
GRAPH TRAVERSAL.
Presentation transcript:

BFS,DFS Topological Sort תרגול 12 BFS,DFS Topological Sort ds162-ps12 28.11.2018

גרפים גרף G=(V,E) 2 דרכים עיקריות לייצג גרף כמבנה נתונים: מטריצת סמיכויות מטריצה M בגודל |𝑉|𝑥|𝑉| M[i,j]=1 אם יש קשת בין 𝑣𝑖 ל𝑣𝑗 בG, אחרת M[i,j]=0 רשימות סמיכויות מערך בגודל |V| התא ה𝑖 מכיל רשימה משורשרת של השכנים של 𝑣𝑖. כלומר, של כל הקודקודים 𝑣𝑗 כך ש 𝑣 𝑖 , 𝑣 𝑗 ∈𝐸

ds162-ps12 28.11.2018

ds162-ps12 28.11.2018

BFS(G, s) - Breadth-First Search קודם נגיע לשכנים הקרובים ביותר ורק אח"כ נתרחק. ds162-ps12 28.11.2018

BFS(G, s) - Breadth-First Search מתחילים מקודקוד המקור 𝑠 ועוברים שכבה שכבה עד שמכסים את כל הקודקודים שניתן להגיע מ𝑠 אליהם. האלגוריתם מחשב את המרחק הקצר ביותר מ𝑠 לכל הקודקודים הנגישים לו. האלגוריתם עובד על גרף מכוון ולא מכוון. ds162-ps12 28.11.2018

BFS(G,s) 1. for each vertex u in V[G] – {s} 2 do color[u]  white 3 d[u]   4 [u]  null 5 color[s]  gray 6 d[s]  0 7 [s]  null 8 Q   9 enqueue(Q,s) 10 while Q   11 do u  dequeue(Q) 12 for each v in Adj[u] 13 do if color[v] = white 14 then color[v]  gray 15 d[v]  d[u] + 1 16 [v]  u 17 enqueue(Q,v) 18 color[u]  black white: undiscovered (לא נגעתי) gray: discovered (בטיפול) black: finished(סיימתי) Q: a queue of discovered vertices color[v]: color of v d[v]: distance from s to v [u]: predecessor of v ds162-ps12 28.11.2018

Example (BFS) r s t u        v w x y Q: s ds162-ps12 28.11.2018

Example (BFS) r s t u 1    1   v w x y Q: w r 1 1 ds162-ps12    1   v w x y Q: w r 1 1 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2   1 2  v w x y Q: r t x 1 2 2 ds162-ps12 2   1 2  v w x y Q: r t x 1 2 2 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2  2 1 2  v w x y Q: t x v 2 2 2 ds162-ps12 2  2 1 2  v w x y Q: t x v 2 2 2 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2 3 2 1 2  v w x y Q: x v u 2 2 3 ds162-ps12 2 3 2 1 2  v w x y Q: x v u 2 2 3 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2 3 2 1 2 3 v w x y Q: v u y 2 3 3 ds162-ps12 2 3 2 1 2 3 v w x y Q: v u y 2 3 3 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2 3 2 1 2 3 v w x y Q: u y 3 3 ds162-ps12 2 3 2 1 2 3 v w x y Q: u y 3 3 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2 3 2 1 2 3 v w x y Q: y 3 ds162-ps12 2 3 2 1 2 3 v w x y Q: y 3 ds162-ps12 28.11.2018

Example (BFS) r s t u 1 2 3 2 1 2 3 v w x y Q:  ds162-ps12 28.11.2018

Time complexity: O(|V| + |E|), sometimes written as O(V+E) Example (BFS) r s t u 1 2 3 2 1 2 3 v w x y BFS Tree Time complexity: O(|V| + |E|), sometimes written as O(V+E) ds162-ps12 28.11.2018

DFS(G, s) - Depth-First Search ds162-ps12 28.11.2018

DFS DFS(G) 1. for each vertex u  V[G] 2. do color[u]  white 3. [u]  NULL 4. time  0 5. for each vertex u  V[G] 6. do if color[u] = white 7. then DFS-Visit(u) DFS-Visit(u) color[u]  GRAY time  time + 1 d[u]  time for each v  Adj[u] do if color[v] = WHITE then [v]  u DFS-Visit(v) color[u]  BLACK f[u]  time Uses a global timestamp time. ds162-ps12 28.11.2018

Example (DFS) u v w 1/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/ 3/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/ 4/ 3/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/ B 4/ 3/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/ B 4/5 3/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/ B 4/5 3/6 x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/7 B 4/5 3/6 x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/ 2/7 B F 4/5 3/6 x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 B F 4/5 3/6 x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 9/ B F 4/5 3/6 x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 9/ B C F 4/5 3/6 x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 9/ B C F 4/5 3/6 10/ x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 9/ B C F 4/5 3/6 10/ B x y z ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 9/ B C F 4/5 3/6 B x y z 10/11 ds162-ps12 28.11.2018

Example (DFS) u v w 1/8 2/7 9/12 B C F 4/5 3/6 B x y z 10/11 ds162-ps12 28.11.2018

DFS Edges Classifications DFS can be used to classify the edges of the input graph G=(V,E). Tree-edges (u,v) - if v was initially discovered using edge from u to v Back-edges (u,v) - if v is an ancestor of u in the depth-tree Forward-edges (u,v) - not a tree-edge, where u is v's ancestor Cross-edges (u,v) - All the other edges, where u and v are vertices in different depth-tree, u is not v's ancestor or v is not u's ancestor ds162-ps12 28.11.2018

DFS Edges Classifications מה קורה בגרף לא מכוון? DFS can be used to classify the edges of the input graph G=(V,E). Tree-edges (u,v) - if v was initially discovered using edge from u to v Back-edges (u,v) - if v is an ancestor of u in the depth-tree Forward-edges (u,v) - not a tree-edge, where u is v's ancestor Cross-edges (u,v) - All the other edges, where u and v are vertices in different depth-tree, u is not v's ancestor or v is not u's ancestor הקשתות היחידות הן: קשתות עץ קשתות אחורה ds162-ps12 28.11.2018

דוגמת ריצה של BFS & DFS ds162-ps12 28.11.2018

דוגמת ריצה של BFS & DFS ds162-ps12 28.11.2018

רעיון – בוא נקבע סדר כלשהו על הגרף שאלה 1 – גרף חסר מעגלים נתון גרף לא מכוון𝐺 = (𝑉,𝐸) המיוצג ע"י רשימת שכנויות. הצע אלגוריתם ליצירת גרף מכוון חסר מעגלים 𝐺′ = (𝑉,𝐸′) מתוך הגרף 𝐺. האלגוריתם קובע את הכיוון של הקשתות 𝐸 כך ש|E′|=|E|. רעיון – בוא נקבע סדר כלשהו על הגרף ds162-ps12 28.11.2018

שאלה 1 – גרף חסר מעגלים פתרון: נמספר את כל הקודקודים בגרף. נתון גרף לא מכוון𝐺 = (𝑉,𝐸) המיוצג ע"י רשימת שכינויות. הצע אלגוריתם ליצירת גרף מכוון חסר מעגלים 𝐺′ = (𝑉,𝐸′) מתוך הגרף 𝐺. האלגוריתם קובע את הכיוון של הקשתות 𝐸 כך ש|E′|=|E|. פתרון: נמספר את כל הקודקודים בגרף. נאתחל 𝐸 ′ = . לכל קודקוד 𝑣 𝑖 ∈𝑉 וקשת 𝑣 𝑖 , 𝑣 𝑗 ∈𝐸 כך ש 𝑖<𝑗 אזי: 𝐸 ′ ← 𝐸 ′ ∪( 𝑣 𝑖 , 𝑣 𝑗 ) ds162-ps12 28.11.2018

שאלה 1 – גרף חסר מעגלים הוכחה: נוכיח כי 𝐸 ′ = 𝐸 : נוכיח כי 𝐸 ′ = 𝐸 : לכל קשת 𝑣 𝑖 , 𝑣 𝑗 ∈𝐸 מתקיים כי או (𝑖 < 𝑗) או 𝑗 < 𝑖 . מכיוון ש 𝑣 𝑖 , 𝑣 𝑗 וגם 𝑣 𝑗 , 𝑣 𝑖 נמצאות ברשימת השכינויות של הגרף הלא מכוון, אז בדיוק אחת מהן תופיע ב𝐸′. ds162-ps12 28.11.2018

שאלה 1 – גרף חסר מעגלים הוכחה: נוכיח כי 𝐺′ הוא גרף חסר מעגלים : נניח בשלילה שיש מעגל ב𝐺′ אשר מתחיל מהקודקוד 𝑣 𝑖 . המעגל עובר בקודקודים 𝑣 𝑖 , 𝑣 𝑖 1 , 𝑣 𝑖 2 ,…, 𝑣 𝑖 𝑘 ואז חוזר ל 𝑣 𝑖 . 𝑣 𝑖 , 𝑣 𝑖 1 , 𝑣 𝑖 2 ,…, 𝑣 𝑖 𝑘 , 𝑣 𝑖 לפי בניית הגרף, מתקיים 𝑖< 𝑖 1 <…< 𝑖 𝑘 <𝑖 סתירה ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות דרך מהעיר A לעיר B מיוצג ע"י הזוג (A,B) הניחו כי הדרכים הם דו כיווניות בהינתן קבוצה של m דרכים ו – n ערים הציעו איך לעבד את הקלט בזמן O(n+m) וזיכרון לא מוגבל כדי לתמוך בפעולה הבאה בזמן O(1): Reachable(i,j)- מחזיר TRUE אם קיימת דרך מעיר i לעיר j אחרת יחזיר FALSE ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות רעיון: רכיבי קשירות: גרף חלקי לגרף המקורי בו קיימת דרך בין כל שני קודקודים בתת הגרף (כל קודקוד בדרך שייך לתת הגרף) ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות עיבוד מקדים: ייצג את הקלט כגרף לא מכוון G=(V,E), כך שהקודקודים ייצגו את הערים והצלעות את הדרכים. נייצג את הגרף כרשימת סמיכויות. זמן עיבוד וזיכרון : O(n + m) הגדר מערך C בגודל n. המערך ישמור את הנציג של רכיב הקשירות עבור כל קודקוד. עבור כל קודקוד v בגרף G: אם ל v אין נציג, הוא חלק מרכיב קשירות אשר לא התגלה. הפעל DFS או BFS מ – v וסמן במערך C את v כנציג של כל הקודקודים שהתגלו בהרצת הDFS/BFS. אחרת אם ל v יש נציג, הוא חלק מרכיב קשירות שכבר התגלה, לכן נדלג עליו. ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 2 – רכיבי קשירות זמן הריצה של העיבוד המקדים הוא O(n+m) מכיוון שבכל קודקוד נבקר פעמיים - פעם אחת בלולאה החיצונית (סעיף 3 באלגוריתם), ועוד פעם כאשר נבקר בו בזמן שנריץ BFS\DFS מאחד הקודקודים ברכיב הקשירות. בנוסף נבקר בכל צלע פעם אחת. Reachable(i,j): יחזיר TRUE אמ"מ קודקוד i וקודקוד j שייכים לאותו רכיב קשירות, כלומר C[i] = C[j] זמן ריצה : O(1) ds162-ps12 28.11.2018

האם זיכרון לא מוגבל יכול לעזור כאשר הזמן ריצה מוגבל? שאלה 2 – רכיבי קשירות זמן הריצה של העיבוד המקדים הוא O(n+m) מכיוון שבכל קודקוד נבקר פעמיים - פעם אחת בלולאה החיצונית (סעיף 3 באלגוריתם), ועוד פעם כאשר נבקר בו בזמן שנריץ BFS\DFS מאחד הקודקודים ברכיב הקשירות. בנוסף נבקר בכל צלע פעם אחת. Reachable(i,j): יחזיר TRUE אמ"מ קודקוד i וקודקוד j שייכים לאותו רכיב קשירות, כלומר C[i] = C[j] זמן ריצה : O(1) האם זיכרון לא מוגבל יכול לעזור כאשר הזמן ריצה מוגבל? לא, הזיכרון משפיע על זמן הריצה, אם אני רצים בזמן O(n) אזי נוכל להשתמש בלכל היותר O(n) זיכרון ds162-ps12 28.11.2018

שאלה 3 – רדוקציה רדוקציה - תזכורת: נניח שיש לנו שתי בעיות 𝑃 1 , 𝑃 2 . נניח שיש לנו שתי בעיות 𝑃 1 , 𝑃 2 . יש לנו אלגוריתם שפותר את 𝑃 2 . רדוקציה מבעיה 𝑃 1 לבעיה 𝑃 2 זוהי דרך לפתור את הבעיה 𝑃 1 בעזרת האלגוריתם לפתרון 𝑃 2 ds162-ps12 28.11.2018

שאלה 3 – רדוקציה בהינתן גרף לא מכוון G = (V,E), (G קשיר). נגיד עבור כל 2 קבוצות של קודקודים 𝑉1 ו 𝑉2 כך ש 𝑉 1 ∪ 𝑉 2 ⊆𝑉 : distance(u,v) – אורך המסלול הקצר ביותר מ u ל-v ב G. distance(V1, V2) – min 𝑣∈ 𝑉 1 ,𝑢∈ 𝑉 2 {𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑢,𝑣 } אורך המסלול הקצר ביותר מקודקוד ב 𝑉 1 לקודקוד ב 𝑉 2 (הערה: אם 𝑉1∩ 𝑉 2 ≠∅ אז distance(𝑉1, 𝑉2)=0) מצא את distance(V1, V2) בזמן ריצה 𝑶(|𝑽|+|𝑬|) 1 3 4 5 2 6 ds162-ps12 28.11.2018

שאלה 3 – רדוקציה פתרון: נגדיר גרף חדש G'=(V',E') מהגרף הישן G = (V,E). נוסיף 2 קודקודים s ו t ל V, נחבר את s לכל קודקוד ב 𝑉 1 ונחבר את t לכל קודקוד ב 𝑉 2 ונריץBFS כדי לגלות את אורך המסלול הקצר ביותר מ s ל t. אורך המסלול הקצר ביותר מ s ל t יהיה distance(V1, V2) + 2 1 s 3 4 5 t 2 6 ds162-ps12 28.11.2018

שאלה 3 – רדוקציה Solution: 𝑉 ′ ←𝑉∪ 𝑠,𝑡 𝐸 ′ ←𝐸∪ 𝑠,𝑢 :𝑢∈ 𝑉 1 ∪ 𝑣,𝑡 :𝑣∈ 𝑉 2 Execute BFS from s // Finding d(t). return 𝑑(𝑡) – 2 Runtime: 𝑂(|𝑉|) + 𝑂(|𝑉|) + 𝑂(|𝑉| + |𝐸|) + 𝑂(1) = 𝑂(|𝑉| + |𝐸|) ds162-ps12 28.11.2018

שאלה 4 הצע אלגוריתם הקובע האם בגרף לא מכוון קיים מעגל (באורך גדול מ-1) הרץ בזמן ריצה 𝑂(|𝑉|) שימו לב כי זמן הריצה אינו תלוי ב E ds162-ps12 28.11.2018

שאלה 4 פתרון: טענה: גרף לא מכוון הוא ללא מעגלים אמ"מ בפעלת DFS על הגרף לא נקבל צלעות לאחור. צלע (u,v) נקראת צלע לאחור אם u אב קדמון של v אך הצלע (u,v) אינה צלע בעץ שנוצר ע"י ה-DFS ds162-ps12 28.11.2018

שאלה 4 פתרון: טענה: גרף לא מכוון הוא ללא מעגלים אמ"מ בפעלת DFS על הגרף לא נקבל צלעות לאחור. ניתן לשנות את אלגוריתם ה-DFS כך שהוא יסווג קשתות כשהוא נתקל בהם. בDFS של גרף לא מכוון כל צלע היא צלע בעץ או צלע לאחור. אם הגענו לקודקוד אפור (הגענו דרך צלע לאחור) חזרנו לקודקוד שכבר ביקרנו בו ולכן בגרף יש מעגל. ds162-ps12 28.11.2018

שאלה 4 Solution: Acyclic(G=(V,E)) execute DFS on the graph while: counting the number of visited nodes If DFS visits a grey node return "cycle" זמן ריצת האלגוריתם הוא O(|V|), מכיוון שבמקרה הגרוע ביותר האלגוריתם יסרוק |V|-1 קודקודים לפני שיגיע לקודקוד אפור (קשת לאחור) או שהאלגוריתם יסתיים (בגלל שגרף לא מכוון עם יותר מ|V| קשתות מכיל מעגל) ds162-ps12 28.11.2018

שאלה 5 הגדרה: קודקוד בגרף מכוון נקרא super-sink אם דרגת היציאה שלו היא 0, ודרגת הכניסה שלו היא |V|-1 דוגמה: ds162-ps12 28.11.2018

שאלה 5 נתון גרף מכוון G=(V,E) המיוצג בעזרת מטריצת סמיכויות. הציעו אלגוריתם שרץ בזמן O(|V|) המוצא האם קיים super-sink בגרף, ואם כן מחזיר אותו. רעיון: אם הקודקוד vi הוא super-sink, אז כל השורה שלו היא אפסים, וכל העמודה שלו (פרט לתא [i,i]) היא אחדות. i x x 1 x 0 0 0 0 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: 8 7 6 5 4 3 2 1 ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: v6 מועמד יחיד להיות super-sink. למה? 8 7 6 5 4 3 2 1 v6 מועמד יחיד להיות super-sink. למה? ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: נבדוק אם כל השורה אפסים והעמודה אחדות 8 7 6 5 4 3 2 1 נבדוק אם כל השורה אפסים והעמודה אחדות v6 אכן super-sink 28.11.2018

שאלה 5 הדגמת פתרון: במקרה זה 2 מועמד, למה 6 לא יכול להיות מועמד? 8 7 6 4 3 2 1 במקרה זה 2 מועמד, למה 6 לא יכול להיות מועמד? ds162-ps12 28.11.2018

שאלה 5 הדגמת פתרון: במקרה זה 2 מועמד, למה 6 לא יכול להיות מועמד? 8 7 6 4 3 2 1 כי היה צריך להיות פה 1 כדי ש 6 יהיה super-sink במקרה זה 2 מועמד, למה 6 לא יכול להיות מועמד? ds162-ps12 28.11.2018

שאלה 5 פתרון: זמן ריצה: הלולאה לוקחת לכל היותר (n+n) צעדים, ובדיקת כל השורה והעמודה לוקח 2n צעדים. סה"כ T(n)=O(n). ds162-ps12

מיון טופולוגי Topological-Sort סידור קודקודים של גרף מכוון חסר מעגלים – directed acyclic graph (DAG) , 𝐺= 𝑉,𝐸 כך ש: אם יש מסלול מ𝑣 ל𝑢 ב𝐺 אז 𝑣 מופיע לפני 𝑢 בסידור. יכולים להיות מספר פתרונות לבעיה, לדוגמא: בצע DFS על הגרף לחישוב 𝑓 𝑣 כשהביקור בכל קודקוד נגמר והוא נצבע בשחור, הכנס אותו כראש רשימה מקושרת. החזר את הרשימה המקושרת של הקודקודים. Time Complexity: O(|V|+|E|) דוגמא

מיון טופולוגי הדגמה: מיון: מיון אחר: v1 v4 v2 v3 v6 v5 v1 v2 v4 v5 v3

שאלה 6 נתונות 2 רשימות הציעו 2 אלגוריתמים, לבניית מערכת עבור רשימה A של קורסים נדרשים לתואר רשימה B של קדימויות, המכילה זוגות סדורים (x,y), שמציין שקורס x קדם של קורס y. הקדימויות הן ללא מעגלים. הציעו 2 אלגוריתמים, לבניית מערכת עבור סטודנט עצלן שרוצה ללמוד רק קורס אחד בסמסטר סטודנט חרוץ שרוצה לסיים את כל הקורסים מוקדם ככל האפשר, ומוכן ללמוד את כל הקורסים שאפשר במקביל. ds162-ps12 28.11.2018

שאלה 6 דוגמה: { Alg, Eng, Ds1, Ds2, Mat, Ph1, Ph2 } = A = B { (Alg, Ds2), (Ds1, Ds2), (Mat, Ds1), (Ph1, Ph2) } ds162-ps12 28.11.2018

שאלה 6 פתרון לסטודנט עצלן: נסתכל על הרשימות כגרף מכוון, כאשר הקורסים הינם הקודקודים והקדימויות הינם הצלעות. נמיין את הגרף טופולוגית, ואז נעבור עליו לפי הסדר, וכל קורס יהיה בסמסטר. ds162-ps12 28.11.2018

שאלה 6 פתרון לסטודנט עצלן, דוגמה: Mat Alg Ds1 Ds2 Ph1 Ph2 Eng 1 2 3 4 5 6 7 ds162-ps12 28.11.2018

שאלה 6 אלגוריתם חלופי למיון טופולוגי: נעבור על הקודקודים ונשמור את דרגת הכניסה שלהם – O(|V|+|E|) ניצור רשימה של כל הקודקודים עם דרגת כניסה 0 – O(|V|) בלולאה: עוברים על כל הקודקודים בדרגת כניסה 0, מכניסים אותם לרשימת המיון הטופולוגי, ומורידים 1 מכל השכנים שלהם, תוך כדי יצירת רשימה חדשה של קודקודים עם דרגה 0 O(d(v)) לכל קודקוד, לכן סה"כ לכל הלולאה – O(|V|+|E|)

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: ds162-ps12

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: סיבוב 0: Eng Mat Alg 1 Ds1 2 Ds2 Ph1 Ph2 1 סיבוב 0: קודקודים עם דרגת כניסה 0: Eng Alg Mat Ph1 תוצאה:

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: סיבוב 1: Eng Mat Alg Ds1 1 Ds2 Ph1 Ph2 סיבוב 1: קודקודים עם דרגת כניסה 0: Ds1 Ph2 תוצאה: Eng Alg Mat Ph1

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: סיבוב 2: Eng Mat Alg Ds1 Ds2 Ph1 Ph2 סיבוב 2: קודקודים עם דרגת כניסה 0: Ds2 תוצאה: Eng Alg Mat Ph1 Ds1 Ph2

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: Eng Mat Alg Ds1 Eng Mat Alg Ds1 Ds2 Ph1 Ph2 תוצאה: Eng Alg Mat Ph1 Ds1 Ph2 Ds2

שאלה 6 פתרון לסטודנט החרוץ: נבצע את המיון הטופולוגי הנ"ל, כך שאת כל הקודקודים עם דרגת כניסה 0 בסיבוב הi- נכניס לסמסטר הi+1. ds162-ps12 28.11.2018

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: סיבוב 0: Eng Mat Alg 1 Ds1 2 Ds2 Ph1 Ph2 1 סיבוב 0: Eng Alg Mat Ph1 קודקודים עם דרגת כניסה 0: תוצאה: ds162-ps12

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: סיבוב 1: Eng Mat Alg Ds1 1 Ds2 Ph1 Ph2 סיבוב 1: קודקודים עם דרגת כניסה 0: Eng Ds1 Ph2 Alg Mat תוצאה: ds162-ps12 Ph1

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: סיבוב 2: Eng Mat Alg Ds1 Ds2 Ph1 Ph2 סיבוב 2: קודקודים עם דרגת כניסה 0: Ds2 Eng Alg Mat Ds1 תוצאה: ds162-ps12 Ph1 Ph2

שאלה 6 אלגוריתם חלופי למיון טופולוגי, הדגמה: תוצאה: Eng Mat Alg Ds1 Eng Mat Alg Ds1 Ds2 Ph1 Ph2 Eng Alg Mat Ds1 תוצאה: ds162-ps12 Ph1 Ph2 Ds2

שאלה 7 נתון DAG (directed acyclic graph - גרף מכוון ללא מעגלים), בו כל הצלעות ממושקלות. הציעו אלגוריתם בזמן O(|V|+|E|) למציאת המשקל של המסלול עם המשקל המקסימלי. דוגמה: 1 6 3 5 2 10 המשקל המקסימלי של מסלול כלשהו בגרף הוא 13 4 8 ds162-ps12 28.11.2018

A[i] = max{A[j]+w(vj,vi) : (vj,vi)E } שאלה 7 נתון DAG (directed acyclic graph - גרף מכוון ללא מעגלים), בו כל הצלעות ממושקלות. הציעו אלגוריתם בזמן O(|V|+|E|) למציאת המשקל של המסלול עם המשקל המקסימלי. רעיון הפתרון: נמיין את הגרף טופולוגית, ואז בכל קודקוד, המשקל המקסימלי של מסלול עד אליו הוא המקסימום של {המשקל המקסימלי של מסלול עד קודקוד עם קשת אליו + משקל הקשת} A[i] = max{A[j]+w(vj,vi) : (vj,vi)E } v1 v2 v4 v5 v3 v6 1 2 3 4 5 6 ds162-ps12 28.11.2018

שאלה 7 פתרון: נמיין את G טופולוגית – O(|V|+|E|). נסמן את המיון כ{v1,…,vn} לכל קודקוד v נבנה רשימה של in-vertices, כלומר קודקודים u כך ש(u,v)∈E – O(|V|+|E|) נגדיר מערך A בגודל |V|, כך שבסיום יתקיים A[i] = משקל המסלול הכבד ביותר המסתיים בvi עבור i מ1 עד n נחשב A[i]=max{A[j]+w(vj,vi)|(vj,vi)∈E} (נשים לב שכיוון שמיינו טופולוגית, כל הA[j] הרלוונטיים כבר חושבו כאשר נרצה לחשב את A[i]) נחזיר את max{A[1],…,A[n]} – O(|V|) ds162-ps12 28.11.2018

שאלה 7 MaxPath(G) v1, v2, …, vn ← TopologicalSort(G) Create-in-edges-lists() A ← new array [1..n] A[1] ← 0 for i ← 2 to n do A[i] ← max{A[j]+w(j,i) : j  in_edges_list[i] } return max{A[1],A[2],…,A[n]} ds162-ps12 28.11.2018

שאלה 7 We design the algorithm: Express the solution to a problem in terms of solutions to smaller problems. Solve all the smallest problems first and put their solutions in a table Solve the next larger problems, and so on, up to the problem, we originally wanted to solve. Each problem should be easily solvable by looking up and combining solutions of smaller problems in the table. Time Complexity: O(|V|+|E|) v1 v2 v4 v5 v3 v6 1 2 3 4 5 6 ds162-ps12 28.11.2018

שאלה 7 הדגמת פתרון: v2 1 v1 6 A 3 5 v5 2 10 1 v4 v3 2 1 3 5 4 8 v6 4 4 5 7 v1 v2 v3 v4 v5 v6 1 2 4 5 10 3 8 6 6 13 כיצד ניתן לשנות את האלגוריתם כך שגם יחזיר את המסלול הכבד ביותר? ds162-ps12 28.11.2018

תרגול בבית Update the BFS algorithm so that every vertex v in the graph contains not only d[v], the length of shortest path from s to v, but also the number of different paths of that length. ds162-ps12 28.11.2018

תרגול בבית Solution: Each node u in the graph will contain an extra field: M(u) - the number of different paths to u of length d(u). Initialize M(v)0 for all the vertices except s. initialize M(s)1. For each vertex u, when going over the neighbors v of u: if v is a white neighbor then M(v)  M(u) else // v is not a white neighbor of u if d(v) = d(u)+1 M(v)  M(v) + M(u) ds162-ps12 28.11.2018