CS 401: Computer Algorithm I BFS Xiaorui Sun 1
This course Grading Scheme • Homework ~ 50% ( Five problem sets due on Wed 4pm) • Midterm ~ 20% • Final ~ 30% • Submit through GradeScope (https://www.gradescope.com/courses/169509) • Ask question in the class • Use Blackboard raise hand function • All the lectures will be recorded (watch through Blackboard) • Read the textbook! (Algorithm Design by Kleinberg and Tardos) • Website: http://www.cs.uic.edu/~xiaorui/cs401 • Lecture slides, homework • Piazza: piazza.com/uic/fall2020/2020fallcs40141675 • Announcements, online discussion forum • TA will answer course related questions • Office hours: • Myself: Fri 2pm-4pm • Wenyu Jin: Tue 10am-12pm (wjin9@uic.edu) 2
Administrativia Stuffs • Homework 1 is out (due Sep 23) Guidelines: • You can collaborate, but you must write solutions on your own • Your algorithms/proofs should be clear, well-organized, and concise. Spell out main idea. • Sanity Check: Make sure you use assumptions of the problem • You CANNOT search the solution online. • Late homework will be penalized at a rate of 10% of the initial grade per late day. • Correctness proofs of ALGORITHMS are NOT REQUIRED, but you are encouraged to prove the correctness. • Example: stable matching 3
Recap: Undirected graph Notation. G = (V, E) • V = nodes (or vertices) • E = edges between pairs of nodes • Captures pairwise relationship between objects • Graph size parameters: n = |V|, m = |E| Terminology: path, cycle, tree, degree, connected component
#edges Let ! = ($, &) be a graph with ( = |$| vertices and * = & edges. . = - -/0 - = 1(( . ) Claim: 0 ≤ * ≤ . Pf: Since every edge connects two distinct vertices (i.e., G has no loops) and no two edges connect the same pair of vertices (i.e., G has no multi-edges) It has at most - . edges. 5
Degree 1 vertices Claim: If G has no cycle, then it has a vertex of degree ≤ 1 (Every tree has a leaf) Proof: (By contradiction) Suppose every vertex has degree ≥ 2. Start from a vertex & ' and follow a path, & ' , … , & * when we are at & * we choose the next vertex to be different from & *+' . We can do so because deg & * ≥ 2. The first time that we see a repeated vertex ( & / = & * ) we get a cycle. We always get a repeated vertex because 2 has finitely many vertices & 4 & ' & 5 & 3 & 6 6
Trees and Induction Claim: Show that every tree with ! vertices has ! − 1 edges. Proof: (Induction on ! .) Base Case: ! = 1 , the tree has no edge Inductive Step: Let % be a tree with ! vertices. So, % has a vertex & of degree 1 . Remove & and the neighboring edge, and let %’ be the new graph. We claim %’ is a tree: It has no cycle, and it must be connected. So, %’ has ! − 2 edges and % has ! − 1 edges. 7
Graph Traversal Walk (via edges) from a fixed starting vertex ! to all vertices reachable from ! . • Breadth First Search (BFS): Order nodes in successive layers based on distance from ! • Depth First Search (DFS): More natural approach for exploring a maze; Applications of BFS: • Finding shortest path for unit-length graphs • Finding connected components of a graph • Testing bipartiteness 8
Breadth First Search (BFS) Completely explore the vertices in order of their distance from ! . Three states of vertices: • Undiscovered • Discovered • Fully-explored Naturally implemented using a queue The queue will always have the list of Discovered vertices 9
BFS algorithm Initialization: mark all vertices "undiscovered" BFS( ! ) mark ! discovered queue = { ! } while queue not empty " = remove_first(queue) for each edge {", %} if ( % is undiscovered) mark % discovered append % on queue mark " fully-explored 10
BFS(1) 1 Queue: 1 2 3 4 6 7 9 5 11 12 10 8 13 11
BFS(1) 1 Queue: 2 3 2 3 4 6 7 9 5 11 12 10 8 13 12
BFS(1) 1 Queue: 3 4 2 3 4 6 7 9 5 11 12 10 8 13 13
BFS(1) 1 Queue: 4 5 6 7 2 3 4 6 7 9 5 11 12 10 8 13 14
BFS(1) 1 Queue: 5 6 7 8 9 2 3 4 6 7 9 5 11 12 10 8 13 15
BFS(1) 1 Queue: 7 8 9 10 2 3 4 6 7 9 5 11 12 10 8 13 16
BFS(1) 1 Queue: 8 9 10 11 2 3 4 6 7 9 5 11 12 10 8 13 17
BFS(1) 1 Queue: 9 10 11 12 13 2 3 4 6 7 9 5 11 12 10 8 13 18
BFS(1) 1 Queue: 2 3 4 6 7 9 5 11 12 10 8 13 19
Graph representation Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. • Space proportional to n 2 . • Checking if (u, v) is an edge takes Q (1) time. • Identifying all edges takes Q (n 2 ) time.
BFS Analysis Initialization: mark all vertices "undiscovered" Graph representation: adjacency matrix BFS( ! ) mark ! discovered O(n) times: At most once per vertex queue = { ! } while queue not empty O(n) times: " = remove_first(queue) Check every vertex % for each edge {", %} if ( % is undiscovered) mark % discovered append % on queue mark " fully-explored Overall: O(n 2 ) time 21
Graph representation Adjacency list. Node indexed array of lists. • Space proportional to m+n. • Checking if (u, v) is an edge takes O (deg(u)) time. • Identifying all edges takes Q (m+n) time.
BFS Analysis Initialization: mark all vertices "undiscovered" Graph representation: adjacency list BFS( ! ) mark ! discovered O(n) times: At most once per vertex queue = { ! } while queue not empty O(deg( " )) times: " = remove_first(queue) At most twice per edge for each edge {", %} if ( % is undiscovered) mark % discovered append % on queue mark " fully-explored Overall: O(n+m) time 23
Recommend
More recommend