Introduction to Graphs and Traversal Tyler Moore CSE 3353, SMU, - - PDF document

introduction to graphs and traversal
SMART_READER_LITE
LIVE PREVIEW

Introduction to Graphs and Traversal Tyler Moore CSE 3353, SMU, - - PDF document

Notes Introduction to Graphs and Traversal Tyler Moore CSE 3353, SMU, Dallas, TX March 7, 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For


slide-1
SLIDE 1

Introduction to Graphs and Traversal

Tyler Moore

CSE 3353, SMU, Dallas, TX

March 7, 2013

Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author

  • f Algorithm Design Manual. For more information see http://www.cs.sunysb.edu/~skiena/

Introduction to Graphs

Many problems can be represented using graphs Once we reduce a problem to a graph, we can often use standard graph algorithms to navigate the graph A graph G = (V , E) is defined by a set of vertices V , and a set of edges E consisting of ordered or unordered pairs of vertices from V Reading for today’s lecture: ADM 145-152 AND Python Algorithms Ch 2. pp. 23–31 and Ch. 5 pp. 101–107, availble from http://lyle.smu.edu/~tylerm/courses/cse3353/pyalg.html Shameless plug: Jailbreaking Information: The Legacy of Hacktivist Aaron Swartz, Fondren Library Mezzanine, TONIGHT 6:30pm-8pm

2 / 28

Social Network as a Graph

Alice Bob Charlie Dawn Eve Fred George Nodes: people Edges: friendship

3 / 28

Road Network as a Graph

Main St 1st Ave 2nd Ave 3rd Ave Main St Elm St Aspen Ave 5th Ave Park St Main St 5th Ave

Nodes: intersections Edges: roads

4 / 28

Notes Notes Notes Notes

slide-2
SLIDE 2

Electronic Circuits as a Graph

B 20Ω 10Ω vx

S 5 vx

5Ω A Vertices: junctions Edges: components

5 / 28

World Wide Web as a Graph

NYT SMU Google Foo DMN Nodes: websites Edges: links

6 / 28

Course Dependencies as a Graph

CSE1341 CSE1342 CSE2353 CSE2341 CSE3353 CSE5338 Nodes: courses Edges: prerequisites

7 / 28

Maze as a Graph

Nodes: rooms; edges: doorways

8 / 28

Notes Notes Notes Notes

slide-3
SLIDE 3

Flavors of Graphs

The first step in any graph problem is recognizing that you have a graph problem It’s not always obvious! The second step in any graph problem is determining which flavor of graph you are dealing with Learning to talk the talk is an important part of walking the walk The flavor of graph has a big impact on which algorithms are appropriate and efficient

9 / 28

Directed vs. Undirected Graphs

A B C D E F G Undirected A B C D E F G Directed A graph G = (V , E) is undirected if edge (x, y) ∈ E implies that (y, x) is also in E Road networks between cities are typically undirected Street networks within cities are almost always directed because of

  • ne-way streets

What about online social networks?

10 / 28

Weighted vs. Unweighted Graphs

A B C D E F G Unweighted A B C D E F G

10 4 1 7 5 8 19 4 10 6 8

Weighted In weighted graphs, each edge (or vertex) of G is assigned a numerical value, or weight The edges of a road network graph might be weighted with their length, drive-time or speed limit In unweighted graphs, there is no cost distinction between various edges and vertices

11 / 28

Simple vs. Non-simple Graphs

A B C D E F G Simple A B C D E F G Non-simple Certain types of edges complicate the task of working with graphs. A self-loop is an edge (x, x) involving only one vertex. An edge (x, y) is a multi-edge if it occurs more than once in the graph. Any graph without self-loops and multi-edges is called simple. Which of the earlier example graphs can be non-simple?

12 / 28

Notes Notes Notes Notes

slide-4
SLIDE 4

Sparse vs. Dense Graphs

A B C D E F G Sparse A B C D E F G Dense Graphs are sparse when only a small fraction of the possible number

  • f vertex pairs actually have edges defined between them.

Graphs are usually sparse due to application-specific con- straints Road networks must be sparse because of road junctions Typically dense graphs have a quadratic number of edges while sparse graphs are linear in size

13 / 28

Cyclic vs. Acyclic Graphs

A B C D F G Cyclic A B C D F G Acyclic An acyclic graph does not contain any cycles Trees are connected, acyclic, undirected graphs Directed acyclic graphs are called DAGs DAGs arise naturally in scheduling problems, where a directed edge (x, y) indicates that x must occur before y.

14 / 28

Labeled vs. Unlabeled Graphs

A B C D E F G Labeled Unlabeled In labeled graphs, each vertex is assigned a unique name or identifier to distinguish it from all other vertices. An important graph problem is isomorphism testing: determining whether the topological structure of two graphs are in fact identical if we ignore any labels.

15 / 28

More Graph Terminology

A path is a sequence of edges connecting two vertices Two common problems: (1) does a path exist between A and B? (2) what is the shortest path between A and B? A graph is connected if there is a path between any two vertices A directed graph is strongly connected if there is a directed path between any two vertices. The degree of a vertex is the number of edges adjacent to it

16 / 28

Notes Notes Notes Notes

slide-5
SLIDE 5

Graph Data Structures

a b d c e f h g Option 1: Adjacency Sets

a , b , c , d , e , f , g , h = range (8) N = [ {b , c , d , e , f } , # a {c , e } , # b {d } , # c {e } , # d { f } , # e {c , g , h } , # f { f , h } , # g { f , g} # h ] > > > b i n N[ a ] # Neighborhood membership True > > > l e n (N[ f ] ) # Degree 3

17 / 28

Graph Data Structures

a b d c e f h g Option 2: Adjacency Lists

a , b , c , d , e , f , g , h = range (8) N = [ [ b , c , d , e , f ] , # a [ c , e ] , # b [ d ] , # c [ e ] , # d [ f ] , # e [ c , g , h ] , # f [ f , h ] , # g [ f , g ] # h ] > > > b i n N[ a ] # Neighborhood membership True > > > l e n (N[ f ] ) # Degree 3

17 / 28

Graph Data Structures

a b d c e f h g

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

Option 3: Adjacency Dictionaries w/ Edge Weights

a , b , c , d , e , f , g , h = range (8) N = [ {b : 2 , c : 1 , d : 3 , e : 9 , f :4 } ,#a {c : 4 , e : 3} , #b {d : 8} , #c {e : 7} , #d { f : 5} , #e {c : 2 , g : 2 , h : 2} , #f { f : 1 , h : 6} , #g { f : 9 , g :8} #h ] > > > b i n N[ a ] # Neighborhood membership True > > > l e n (N[ f ] ) # Degree 3 > > > N[ a ] [ b ] # Edge weight f o r ( a , b ) 2

17 / 28

Graph Data Structures

a b d c e f h g Option 4: Dictionaries w/ Adjacency Sets

N = { ’ a ’ : s e t ( ’ bcdef ’ ) , ’ b ’ : s e t ( ’ ce ’ ) , ’ c ’ : s e t ( ’ d ’ ) , ’ d ’ : s e t ( ’ e ’ ) , ’ e ’ : s e t ( ’ f ’ ) , ’ f ’ : s e t ( ’ cgh ’ ) , ’ g ’ : s e t ( ’ fh ’ ) , ’ h ’ : s e t ( ’ fg ’ ) } > > > ’ b ’ i n N[ ’ a ’ ] # Neighborhood membership True > > > l e n (N[ ’ f ’ ] ) # Degree 3

17 / 28

Notes Notes Notes Notes

slide-6
SLIDE 6

Graph Data Structures

a b d c e f h g Option 5: Adjacency Matrix (using nested lists)

a , b , c , d , e , f , g , h = range (8) # a b c d e f g h N = [ [ 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 ] , # a [ 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , # b [ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , # c [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , # d [ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , # e [ 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 ] , # f [ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ] , # g [ 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 ] ] # h > > > a , b , c , d , e , f , g , h = range (8) > > > N[ a ] [ b ] # Neighborhood membership 1 > > > sum(N[ f ] ) # Degree 3

17 / 28

Graph Data Structures

a b d c e f h g

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

Option 6: Weighted Adjacency Matrix (using nested lists)

a , b , c , d , e , f , g , h = range (8) = f l o a t ( ’ i n f ’ ) # a b c d e f g h W = [ [ 0 , 2 , 1 , 3 , 9 , 4 , , ] , # a [ , 0 , 4 , , 3 , , , ] , # b [ , , 0 , 8 , , , , ] , # c [ , , , 0 , 7 , , , ] , # d [ , , , , 0 , 5 , , ] , # e [ , , 2 , , , 0 , 2 , 2 ] , # f [ , , , , , 1 , 0 , 6 ] , # g [ , , , , , 9 , 8 , 0 ] ] # h > > > i n f = f l o a t ( ’ i n f ’ ) > > > W[ a ] [ b ] < i n f # Neighborhood membership True > > > W[ c ] [ e ] < i n f # Neighborhood membership F a l s e > > > sum(1 f o r w i n W[ a ] i f w < i n f ) − 1 # Degree 5

17 / 28

Tradeoffs Between Adjacency Lists and Adjacency Matrices

When the ADM refers to adjacency lists, Skiena means adjacency linked lists Adjacency linked lists use less memory for sparse graphs than matrices Finding vertex degree is Θ(1) vs. Θ(n) in matrices In Python, adjacency lists are technically adjacency dynamic arrays (respectively sets and dictionaries for other representations) Python adjacency lists/sets/dictionaries use less memory than adjacency matrices implemented in Python They are also Θ(1) to find vertex degree Within Python, adjacency sets offer expected Θ(1) membership checking vs. Θ(1) for lists For graph traversal, adjacency lists execute faster than sets.

18 / 28

Graph Traversal

One of the most fundamental graph problems is to traverse every edge and vertex in a graph. For correctness, we must do the traversal in a systematic way so that we dont miss anything. For efficiency, we must make sure we visit each edge at most twice. Since a maze is just a graph, such an algorithm must be powerful enough to enable us to get out of an arbitrary maze.

19 / 28

Notes Notes Notes Notes

slide-7
SLIDE 7

Marking Vertices

The key idea is that we must mark each vertex when we first visit it, and keep track of what have not yet completely explored. Each vertex will always be in one of the following three states:

1

undiscovered the vertex in its initial, virgin state.

2

discovered the vertex after we have encountered it, but before we have checked out all its incident edges.

3

processed the vertex after we have visited all its incident edges.

A vertex cannot be processed before we discover it, so over the course

  • f the traversal the state of each vertex progresses from undiscovered

to discovered to processed.

20 / 28

To Do List

We must also maintain a structure containing all the vertices we have discovered but not yet completely explored. Initially, only a single start vertex is considered to be discovered. To completely explore a vertex, we look at each edge going out of it. For each edge which goes to an undiscovered vertex, we mark it discovered and add it to the list of work to do. Note that regardless of what order we fetch the next vertex to explore, each edge is considered exactly twice, when each of its endpoints are explored.

21 / 28

Correctness of Graph Traversal

Every edge and vertex in the connected component is eventually

  • visited. Why?

Suppose it’s not correct, ie. there exists an unvisited vertex A whose neighbor B was visited. When B was visited, each of its neighbors was added to the list to be

  • processed. Since A is a neighbor of B, it must be visited before the

algorithm completes.

22 / 28

In what order should we process vertices?

1 First-in-first-out: if we use a queue to process discovered vertices, we

use breadth-first search

2 Last-in-first-out: if we stack a queue to process discovered vertices,

we use depth-first search

23 / 28

Notes Notes Notes Notes

slide-8
SLIDE 8

Depth-First Search

Undirected Graph a b c d e f a b c d e f c d b e a f Depth-First Search Tree a b c d e f

1 2 3 4 7 5 6

24 / 28

Recursive Depth-First Search Code

def r e c d f s (G, s , S=None ) : i f S i s None : S = s e t ()# I n i t i a l i z e the h i s t o r y S . add ( s ) # We ’ ve v i s i t e d s for u in G[ s ] : # Explore neighbors i f u in S : continue# Already v i s i t e d : Skip r e c d f s (G, u , S) # New : Explore r e c u r s i v e l y > > > r e c d f s t e s t e d (G, 0) [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7]

25 / 28

Iterative Depth-First Search Code

def i t e r d f s (G, s ) : S , Q = s e t () , [ ] # V i s i t e d −s e t and queue

  • Q. append ( s )

# We plan on v i s i t i n g s while Q: # Planned nodes l e f t ? u = Q. pop () # Get one i f u in S : continue# Already v i s i t e d ? Skip i t S . add (u) # We ’ ve v i s i t e d i t now

  • Q. extend (G[ u ] )

# Schedule a l l neighbors y i e l d u # Report u as v i s i t e d > > > l i s t ( i t e r d f s (G, 0)) [0 , 5 , 7 , 6 , 2 , 3 , 4 , 1]

26 / 28

Breadth-First Search

Undirected Graph a b c d e f a b e f c d a f b e c d Breadth-First Search Tree a b e f c d

1 2 3 4 6 5 7

27 / 28

Notes Notes Notes Notes

slide-9
SLIDE 9

Iterative Breadth-First Search Code

from c o l l e c t i o n s import deque def bfs (G, s ) : P, Q = { s : None } , deque ( [ s ] )# Parents and FIFO queue while Q: u = Q. p o p l e f t () # Constant−time f o r deque for v in G[ u ] : i f v in P: continue # Already has parent P[ v ] = u # Reached from u : u i s parent

  • Q. append ( v )

return P

28 / 28

Notes Notes Notes Notes