breadth-first search and depth-first search · breadth-first search and depth-first search ict...

11
Breadth-First Search and Depth-First Search ICT Officers October 2019 1 / 11

Upload: others

Post on 22-May-2020

29 views

Category:

Documents


0 download

TRANSCRIPT

Breadth-First Search and Depth-First Search

ICT Officers

October 2019

1 / 11

Overview

1 Breadth-First Search (BFS)

2 Depth-First Search (DFS)

3 DFS vs BFS

4 Problems

2 / 11

BFS Introduction

Finds shortest path for unweighted graphsChecks layer by layerUnderlying Data Structure: Queue (FIFO)

3 / 11

BFS Psuedocode

4 / 11

DFS Introduction

Finds shortest path for unweighted graphsExplores along each branch (then backtracks)Underlying Data Structure: Stack (LIFO)

5 / 11

DFS Psuedocode

6 / 11

DFS Exercise

Team A and Team B are competing in a game of basketball. Team A hasn players and Team B has m players, with n + m ≤ 17. Each player has aninteger skill level s between 1 and 108. Define the strength of a set ofplayers as the sum of their individual skill levels. In order to ensure a fairgame, Team A and Team B must have equally strong lineups. In howmany ways can the two teams choose their lineups? (Two lineups areconsidered different if there exists a player who starts in one game, but notin the other.)

7 / 11

DFS Exercise: Solution

Solution. We can represent each starting lineup by a sequence of n + m0’s and 1’s, where a player starts if he/she is assigned a 1. Then, we use aDFS to recursively generate all possible starting lineups (binary strings oflength n + m). Once we have a starting lineup, it is straightforward tocheck for fairness.(Adapted from Crash Course Coding Companion)

8 / 11

DFS vs. BFS

9 / 11

Problems

Problem 1 Once upon a time there was only one router in the well-knowncompany Bmail. Years went by and over time new routers were purchased.Every time they bought a new router, they connected it to one of therouters bought before it. You are given the values pi the index of therouter to which the i th router was connected after being purchased(pi < i). There are n routers in Boogle in total now. Print the sequence ofrouters on the path from the first to the nth router. (CodeForces 1057A)

10 / 11

Problems

Problem 2 Instead of mooing at each-other over long distances, the cowsdecide to equip themselves with walkie-talkies, one for each cow. Thesewalkie-talkies each have a limited transmission radius – a walkie-talkie ofpower P can only transmit to other cows up to a distance of P away (notethat cow A might be able to transmit to cow B even if cow B cannottransmit back, due to cow A’s power being larger than that of cow B).Fortunately, cows can relay messages to one-another along a pathconsisting of several hops, so it is not necessary for every cow to be able totransmit directly to every other cow. Due to the asymmetrical nature ofthe walkie-talkie transmission, broadcasts from some cows may be moreeffective than from other cows in their ability to reach large numbers ofrecipients (taking relaying into account). Please help the cows determinethe maximum number of cows that can be reached by a broadcastoriginating from a single cow (USACO December 2016 Silver 3)

11 / 11