CS 401: Computer Algorithm I BFS Xiaorui Sun 1 This course - - PowerPoint PPT Presentation

cs 401 computer algorithm i
SMART_READER_LITE
LIVE PREVIEW

CS 401: Computer Algorithm I BFS Xiaorui Sun 1 This course - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CS 401: Computer Algorithm I

BFS

Xiaorui Sun

1

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Administrativia Stuffs

3

  • 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
slide-4
SLIDE 4

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

slide-5
SLIDE 5

#edges

Let ! = ($, &) be a graph with ( = |$| vertices and * = & edges. Claim: 0 ≤ * ≤

  • . = - -/0

.

= 1((.) 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

slide-6
SLIDE 6

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

6

&' &3 &4 &5 &6

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

11

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 1

slide-12
SLIDE 12

12

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 2 3

slide-13
SLIDE 13

13

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 3 4

slide-14
SLIDE 14

14

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 4 5 6 7

slide-15
SLIDE 15

15

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 5 6 7 8 9

slide-16
SLIDE 16

16

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 7 8 9 10

slide-17
SLIDE 17

17

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 8 9 10 11

slide-18
SLIDE 18

18

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue: 9 10 11 12 13

slide-19
SLIDE 19

19

BFS(1)

1 2 3 10 5 4 9 12 8 13 6 7 11

Queue:

slide-20
SLIDE 20

Graph representation

Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge.

  • Space proportional to n2.
  • Checking if (u, v) is an edge takes Q(1) time.
  • Identifying all edges takes Q(n2) time.
slide-21
SLIDE 21

BFS Analysis

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

21

O(n) times: Check every vertex % O(n) times: At most once per vertex

Graph representation: adjacency matrix

Overall: O(n2) time

slide-22
SLIDE 22

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.
slide-23
SLIDE 23

BFS Analysis

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

23

O(deg(")) times: At most twice per edge O(n) times: At most once per vertex

Graph representation: adjacency list

Overall: O(n+m) time