# Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.

## Presentation on theme: "Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due."— Presentation transcript:

Graphs CSE 331 Section 2 James Daly

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

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

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

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

Weighted Graphs Edges or arcs may have weights V2V2 V1V1 V3V3 V5V5 V4V4 5 10 0 7.5 100 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

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?

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

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

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)

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

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

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

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

DFS(v) [Non-recursive]

BFS(v)

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

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

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)

Download ppt "Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due."

Similar presentations