Graph Theory: Concepts, Algorithms, and Applications, Lecture notes of Algorithms and Programming

[Week 4] Graphs -- Definitions, Representations, BFS, DFS, Directed Graphs

Typology: Lecture notes

2018/2019

Uploaded on 04/20/2019

kefart
kefart 🇺🇸

4.4

(11)

55 documents

1 / 67

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assessment schedule
Assignment 1: due by 11:59 pm tonight
Quiz 1: tutorial time tonight
After Quiz, Tutorial and Help Desk at PNR Lecture Theatre 310 & 311
Quiz 27:00PM Fri 12 April 2019 (Week 7)
Assignment 2 11:00PM Fri 12 April 2019
Quiz 3 7:00PM Fri 24 May 2019 (Week 12)
Assignment 3 11:00PM Fri 24 May 2019
Teaching Assistant
Darcy Townsend [email protected]ey.edu.au
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
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43

Partial preview of the text

Download Graph Theory: Concepts, Algorithms, and Applications and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Assessment schedule

Assignment 1: due by 11:59 pm tonight

Quiz 1: tutorial time tonight

After Quiz, Tutorial and Help Desk at PNR Lecture Theatre 310 & 311

Quiz 2 7:00PM Fri 12 April 2019 (Week 7) Assignment 2 11:00PM Fri 12 April 2019

Quiz 3 7:00PM Fri 24 May 2019 (Week 12) Assignment 3 11:00PM Fri 24 May 2019

Teaching Assistant Darcy Townsend [email protected]

Last Week

  • Data Structures
    • Linked Lists
    • Queue
    • Stack
    • Tree
  • Merge Sort
  • Recursion
  • Master Theorem

Chapter 3

Graphs

Slides by Kevin Wayne.Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

3.1 Basic Definitions and Applications

Some Graph Applications

transportation

Graph street intersections

Nodes Edges highways communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey

software systems functions function calls

scheduling tasks precedence constraints circuits gates wires

World Wide Web

Web graph.  Node: web page.  Edge: hyperlink from one page to another.

cnn.com

netscape.com novell.com cnnsi.com timewarner.com

hbo.com

sorpranos.com

Graph Representation: Adjacency List

Adjacency list. Node indexed array of lists.  Two representations of each edge.  Space proportional to m + n.  Checking if (u, v) is an edge takes O(deg(u)) time.  Identifying all edges takes Θ(m + n) time.

1 2 3 2 3 4 2 5 5 6 7 3 8 8

1 3 4 5 1 2 5 7 8

2 3 4 6 5

degree = number of neighbors of u

3 7

Paths and Connectivity

Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1 , v 2 , …, vk-1 , v (^) k with the property that each consecutive pair v (^) i, v (^) i+1 is joined by an edge in E.

Def. A path is simple if all nodes are distinct.

Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.

Trees

Def. An undirected graph is a tree if it is connected and does not contain a cycle.

Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third.  G is connected.  G does not contain a cycle.  G has n-1 edges.

Rooted Trees

Rooted tree. Given a tree T, choose a root node r and orient each edge away from r.

Importance. Models hierarchical structure.

a tree the same tree, rooted at 1

v

parent of v

child of v

root r

Connectivity

s-t connectivity problem. Given two node s and t, is there a path between s and t?

s-t shortest path problem. Given two node s and t, what is the length of the shortest path between s and t?

Applications.  Many. For example:

  • Fewest number of hops in a communication network.

Breadth First Search

BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time.

BFS algorithm.  L 0 = { s }.  L 1 = all neighbors of L 0.  L 2 = all nodes that do not belong to L 0 or L 1 , and that have an edge to a node in L 1.  L (^) i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L (^) i.

Theorem. For each i, L (^) i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer.

s (^) L 1 L 2 L (^) n-

Breadth First Search

Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1.

L 0

L 1

L 2

L 3

Breadth First Search: Analysis

Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency representation.

Pf.  Easy to prove O(n 2 ) running time:

  • at most n lists L[i]
  • each node occurs on at most one list; for loop runs ≤ n times
  • when we consider node u, there are ≤ n incident edges (u, v), and we spend O(1) processing each edge

 Actually runs in O(m + n) time:

  • when we consider node u, there are deg(u) incident edges (u, v)
  • total time processing edges is Σu∈V deg(u) = 2m

each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)