Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

BFS, DFS, and Graph Problems Introduction, Lecture notes of Data Structures and Algorithms

A lecture note for CSE 373: Data Structures and Algorithms. It covers inter-data relationships, graphs, and their applications. The document also includes examples of adjacency matrix and adjacency list. The lecture also covers BFS, DFS, and graph problems such as s-t path problem. useful as study notes or lecture notes for university students taking CSE 373 or similar courses.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

cristelle
cristelle 🇺🇸

4.5

(53)

375 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 14: BFS, DFS,
Graph problems intro
CSE 373: Data Structures and
Algorithms
CSE 373 20 SP CHAMPION / CHUN 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download BFS, DFS, and Graph Problems Introduction and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Lecture 14: BFS, DFS,

Graph problems intro

CSE 373: Data Structures and Algorithms CSE 373 20 SP – CHAMPION / CHUN 1

Administrivia

Duedate reminders

  • Project 3 due Wednesday May 6th
  • Exercise 3 out tonight, due Friday May 8th

Project 2 hit a little hard

  • Reminder – 7 late days
  • Need a partner? Fill out the partner interest form on piazza
  • Please don’t struggle alone for too long, we’re here to help
  • Fill out the project feedback form on canvas

Midterm grades coming next week

Post-CSE 373 pathways session – fill out your time availability (google form on piazza) so we can

try to choose a time that works for the most people

Introduction to Graphs CSE 373 SP 18 - KASEY CHAMPION 4

Inter-data Relationships

Arrays

Categorically associated

Sometimes ordered

Typically independent

Elements only store pure

data, no connection info

CSE 373 SP 18 - KASEY CHAMPION 5

A
B C

Trees

Directional Relationships

Ordered for easy access

Limited connections

Elements store data and

connection info

A B C

Graphs

Multiple relationship

connections

Relationships dictate

structure

Connection freedom!

Both elements and

connections can store data

A
B
C

Applications

Physical Maps

  • Airline maps
    • Vertices are airports, edges are flight paths
  • Traffic
    • Vertices are addresses, edges are streets Relationships
  • Social media graphs
  • Vertices are accounts, edges are follower relationships
  • Code bases
  • Vertices are classes, edges are usage Influence
  • Biology
  • Vertices are cancer cell destinations, edges are migration paths Related topics
  • Web Page Ranking
  • Vertices are web pages, edges are hyperlinks
  • Wikipedia
  • Vertices are articles, edges are links SO MANY MORREEEE www.allthingsgraphed.com CSE 373 SP 18 - KASEY CHAMPION 7

Graph: Formal Definition

A graph is defined by a pair of sets G = (V, E) where…

  • V is a set of vertices
    • A vertex or “node” is a data entity
  • E is a set of edges
    • An edge is a connection between two vertices CSE 373 SP 18 - KASEY CHAMPION 8
A
B
D C
E
F
G
H

V = { A, B, C, D, E, F, G, H } E = { (A, B), (A, C), (A, D), (A, H), (C, B), (B, D), (D, E), (D, F), (F, G), (G, H)}

Some examples

For each of the following think about what you should choose for vertices and edges. The internet

  • Vertices : webpages. Edges from a to b if a has a hyperlink to b. Family tree
  • Vertices : people. Edges : from parent to child, maybe for marriages too? Input data for the “6 Degrees of Kevin Bacon” game
  • Vertices : actors. Edges : if two people appeared in the same movie
  • Or: Vertices for actors and movies, edge from actors to movies they appeared in. Course Prerequisites
  • Vertices : courses. Edge : from a to b if a is a prereq for b. CSE 373 SU 19 – ROBBIE WEBBER

Adjacency Matrix

0 1 2 3 4 5 6 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 0 2 1 0 0 1 0 0 0 3 0 1 1 0 0 1 0 4 0 0 0 0 0 1 0 5 0 0 0 1 1 0 0 6 0 0 0 0 0 0 0 6 (^2 ) 4 5 0 1 In an adjacency matrix a[u][v] is 1 if there is an edge (u,v), and 0 otherwise. Worst-case Time Complexity (|V| = n, |E| = m): Add Edge: Remove Edge: Check edge exists from (u,v): Get outneighbors of u: Get inneighbors of u: Space Complexity: !(#) !(#) !(#) !(%) !(%) !(%

) CSE 373 SU 19 – ROBBIE WEBBER

Create a Dictionary of size V from type V to Collection of E

If (x,y) ∈ E then add y to the set associated with the key x

Adjacency List

CSE 373 SP 20 - KASEY CHAMPION 13

A
B
C
D

An array where the "

#$

element contains a list of neighbors of ".

Directed graphs: list of out-neighbors (a[u] has v for all (u,v) in E)

Time Complexity (|V| = n, |E| = m):

Add Edge:

Remove Edge (u,v):

Check edge exists from (u,v):

Get neighbors of u (out):

Get neighbors of u (in):

Space Complexity:

Hash Tables

A
B
C
D
C
D
A
B
B

Questions / clarifications on anything? relevant ideas for today

  • vertices, edges, definitions
  • graphs model relationships between real data (you can choose your vertices and edges to
  • different graph implementations exist

Graph problems

There are lots of interesting questions we can ask about a graph:

▪ What is the shortest route from S to T?

▪ What is the longest without cycles?

▪ Are there cycles?

▪ Is there a tour (cycle) you can take that only uses each node (station) exactly once?

▪ Is there a tour (cycle) that uses each edge exactly once?

HANNAH TANG 20WI

Graph problems

Some well known graph problems and their common names:

▪ s-t Path. Is there a path between vertices s and t?

▪ Connectivity. Is the graph connected?

▪ Biconnectivity. Is there a vertex whose removal disconnects the graph?

▪ Shortest s-t Path. What is the shortest path between vertices s and t?

▪ Cycle Detection. Does the graph contain any cycles?

▪ Euler Tour. Is there a cycle that uses every edge exactly once?

▪ Hamilton Tour. Is there a cycle that uses every vertex exactly once?

▪ Planarity. Can you draw the graph on paper with no crossing edges?

▪ Isomorphism. Are two graphs the same graph (in disguise)?

Graph problems are among the most mathematically rich areas of CS theory!

HANNAH TANG 20WI

s-t path Problem

s-t path problem

  • Given source vertex s and a target vertex t , does there exist a path between s and t? 19 1 2 3 4 5 6 7 8 0 s t v What’s the answer for this graph on the left, and how did we get that answer as humans?

v We can see there’s edges that are visually in between s

and t, and we can try out an example path and make sure

that by traversing that path you can get from s to t.

v We know that doesn’t scale that well though, so now

let’s try to define a more algorithmic (comprehensive) way

to find these paths. The main idea is: starting from the

specified s, try traversing through every single possible

path possible that’s not redundant to see if it could lead to

t.

traversals are really important to solving this problem / problems in general, so slight detour to talk about them, we’ll come back to this though

Graph traversals: DFS (should feel similar to 143 in the tree context)

Depth First Search - a traversal on graphs (or on trees since those are also graphs) where you

traverse “deep nodes” before all the shallow ones

High-level DFS: you go as far as you can down one path till you hit a dead end (no neighbors are

still undiscovered or you have no neighbors). Once you hit a dead end, you backtrack / undo

until you find some options/edges that you haven’t actually tried yet.

Kind of like wandering a maze – if you get stuck at a dead end (since you physically have to go and try it out to know it’s a dead end), trace your steps backwards towards your last decision and when you get back there, choose a different option than you did before. one valid DFS traversal: 10, 5, 3, 2, 4, 8, 7,6, 9, 15, 12, 14, 18