Download presentation

Presentation is loading. Please wait.

Published byMiracle Bourn Modified over 6 years ago

1
Graphs CSE 331 Section 2 James Daly

2
Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due next week Friday

3
Graphs G = (V, E) G is a graph V is a set of vertices / nodes E is a set of edges between vertices A vertex u is adjacent to vertex v if and only if the edge uv is in E

4
Graph Example V2V2 V1V1 V3V3 V5V5 V4V4 V1 is connected to V3 but not to V4

5
Digraphs In directed graphs, edges go one way only Directed edges are usually called arcs V2V2 V1V1 V3V3 V5V5 V4V4 V3 is adjacent to V1 But V1 is not adjacent to V3

6
Weighted Graphs Edges or arcs may have weights V2V2 V1V1 V3V3 V5V5 V4V4 5 10 0 7.5 100 7

7
Paths A path is a sequence of vertices v 1, v 2, …, v n such that v i v i+1 is in E for 1 ≤ i < n A simple path is a path where all vertices are distinct (except maybe the first and last) A cycle is a path that starts and ends at the same vertex

8
Directed Acyclic Graphs (DAGs) A DAG is a digraph with no directed cycles All rooted trees are DAGs Is there only ever one path between nodes?

9
Representing Graphs V = {a, b, c, d, e, f, g}|V| E = {(a, b), (a, e), (a, g), (b, c), (b, d), (c, d), (c, e), (d, f), (e, g), (f, e), (f, g)}|E| Space: O(|V| + |E|) a b c d f e g

10
Adjacency List V = {a, b, …, g} a: b: c: … Space: O(|V| + |E|) Good for sparse graphs (few edges) a b c d f e g begacdbde

11
Adjacency Matrix a b c d f e g 0 1 0 0 1 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 1 0 0 1 0 a b c d e f g abcdefgabcdefg 1 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 Good for dense graphs (lots of edges)

12
Degree The degree of a vertex v is the number of edges connected to v In a digraph The out-degree is the number of arcs leaving v The in-degree is the number of arcs entering v

13
Handshaking Lemma 2 + 1 + 1 = 4 = 2 * 2

14
Searching / Traversal Two main methods Depth-first search (DFS) Go as far as possible, then backtrack Uses stacks Breadth-first search (BFS) Check neighborhood first and then spread out Uses queues

15
DFS(v) V1V1 V2V2 V3V3 V4V4 V5V5 DFS(v1):v1v2v4v3v5 V1V2V4V3V5

16
DFS(v) [Non-recursive]

17
BFS(v)

18
BFS V1V1 V2V2 V3V3 V4V4 V5V5 BFS(v1): Distance: v1 0 v2 1 v4 2 v3 1 v5 2 V1V2 V3 V4V5

19
Topological Sort Ordering vertices in a DAG such that if there is a path from u to v then u appears before v in the ordering 260 231 CSE Courses: What order can you take classes to meet your prerequisites? 232 320 410 420 331 450460440 335

20
TopSort(G) Q ← () // Empty queue ForEach v in V: If InDegree(v) = 0 then Q.Enqueue(v) While Q not empty: v ← Q.Dequeue() Process(v) ForEach u in Neighbors(v): InDegree(u)-- If InDegree(u) = 0 Then Q.Enqueue(v)

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google