Week 9 - Friday What did we talk about last time? Graph basics - - PowerPoint PPT Presentation

week 9 friday what did we talk about last time graph
SMART_READER_LITE
LIVE PREVIEW

Week 9 - Friday What did we talk about last time? Graph basics - - PowerPoint PPT Presentation

Week 9 - Friday What did we talk about last time? Graph basics Graph ADT Adjacency matrix representation Wednesdays at 5 p.m. in The Point 113 Saturdays at noon in The Point 113 An adjacency matrix wastes a lot of space


slide-1
SLIDE 1

Week 9 - Friday

slide-2
SLIDE 2

 What did we talk about last time?  Graph basics  Graph ADT  Adjacency matrix representation

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7

 Wednesdays at 5 p.m. in The Point 113  Saturdays at noon in The Point 113

slide-8
SLIDE 8

 An adjacency matrix wastes a lot of space if the graph is not

very dense

 An alternative is an adjacency list  The form of an adjacency list is an array of length n where the

ith element is a pointer to a linked list (or dynamically allocated array) of the nodes adjacent to node i

 This is the approach the book focuses on, since most graphs

are not dense

slide-9
SLIDE 9

2 4 3 1

1 2 3 4

1 1 1 1 2 3 2 3 3 4 4

slide-10
SLIDE 10

2 4 3 1

1 2 3 4

1 2 1 4 3 3 3

slide-11
SLIDE 11

2 4 3 1

It’s a trick!

Some other steps must be taken to represent a multigraph with an adjacency list. Each node in the linked list must contain additional information.

slide-12
SLIDE 12

2 4 3 1

Again, we need extra information in the lists.

5 2 3 6 9 4

1 2 3 4

1 5 0 5 2 2 3 6 4 9 1 2 3 3 1 6 2 3 4 4 1 9 3 4

slide-13
SLIDE 13
slide-14
SLIDE 14

 Similar to a preorder traversal in a tree  We want to visit every node once, going down as far as

possible before backing up

 Issues:

  • No guarantee about ordering like a BST
  • Loops are a problem, how do we keep from repeating nodes?
slide-15
SLIDE 15

2 4 3 1 2 4 3 1 2 4 3 1 2 4 3 1 2 4 3 1 2 4 3 1

We might start at an arbitrary location. What’s a DFS look like that starts at node 4?

4, 1, 0, 2, 3

slide-16
SLIDE 16

 We use pseudocode a lot when describing graph operations, since

the details depend on implementation choice

 Nodes all need some extra information, call it number  Startup

1.

Set the number of all nodes to 0

2.

Pick an arbitrary node u and run DFS( u, 1 )

 DFS( node v, int i )

1.

Set number(v) = i++

2.

Do whatever other processing for v is necessary

3.

For each node u adjacent to v

If number(u) is 0

DFS( u, i )

slide-17
SLIDE 17

 What if we wanted to find paths from node s to other nodes?  We run DFS starting at s with an extra array of length |V|

called edges

 When we move from node u to node v, we set edges[v] = u  Then, to find a path from s to t, we backtrack by looking at

edges[t] and working backwards until we get to s

 This approach will find a path if there is one, but it may not be

the shortest path

slide-18
SLIDE 18

E B A G H D J C I F

Node Edge From A B B C D D A E C F G G E H G I J

To find paths from B to other nodes, we first do a DFS from B E B A G H D J C I F

Working backwards, a path from F to B is: F G E C D A B Thus, a path from B to F is: B A D C E G F

slide-19
SLIDE 19
slide-20
SLIDE 20

 Similar to a level order traversal in a tree  We want to visit every node once, visiting all the children of

  • ne node before moving on to their children

 Similar issues to a DFS

slide-21
SLIDE 21

2 4 3 1 2 4 3 1 2 4 3 1 2 4 3 1 2 4 3 1 2 4 3 1

We might start at an arbitrary location. What’s a BFS look like that starts at node 4?

4, 1, 3, 0, 2

slide-22
SLIDE 22

 More pseudocode!  Nodes all need some extra information, call it number  BFS(node v) 1.

Set the number of all nodes to 0

2.

Create queue q

3.

Set i = 1

4.

number(v) = i++

5.

q.enqueue( v )

6.

While q is not empty

a. v = q.dequeue() b. Do whatever other processing for v is necessary c. For each node u adjacent to v

If number(u) is 0 Set number(u) = i++ q.enqueue(u)

slide-23
SLIDE 23

 Let V be the set of vertices and E be the set of edges  Thus, |V| is the number of vertices and |E| is the number of edges  If you are using adjacency lists then:

  • DFS is:

▪ O(|V| + |E|)

  • BFS is:

▪ O(|V| + |E|)

 If you are using an adjacency matrix then:

  • DFS is:

▪ O(|V|2)

  • BFS is:

▪ O(|V|2)

slide-24
SLIDE 24
slide-25
SLIDE 25

 We have spent a huge amount of time on trees in this class  Trees have many useful properties  What is the only difference between a tree and a graph?  Cycles

  • Well, technically a tree is also connected
slide-26
SLIDE 26

 Is a graph a tree?  It might be hard to tell  We need to come up with an algorithm for detecting any

cycles within the possible tree

 What can we use?  Depth First Search!

slide-27
SLIDE 27

 Nodes all need some extra information, call it number  Startup

1.

Set the number of all nodes to 0

2.

Pick an arbitrary node u and run Detect( u, 1 )

 Detect( node v, int i )

1.

Set number(v) = i++

2.

For each node u adjacent to v

If number(u) is 0

Detect( u, i )

Else

Print “Cycle found!”

slide-28
SLIDE 28

 Even graphs with unconnected components can have cycles  To be sure that there are no cycles, we need to run the

algorithm on every starting node that hasn’t been visited yet

slide-29
SLIDE 29
slide-30
SLIDE 30
slide-31
SLIDE 31

 A directed acyclic graph (DAG) is a directed graph without cycles

in it

  • Well, obviously

 These can be used to represent dependencies between tasks  An edge flows from the task that must be completed first to a task

that must come after

 This is a good model for course sequencing

  • Especially during advising

 A cycle in such a graph would mean there was a circular

dependency

 By running topological sort, we discover if a directed graph has a

cycle, as a side benefit

slide-32
SLIDE 32

 A topological sort gives an ordering of the tasks such that all

tasks are completed in dependency ordering

 In other words, no task is attempted before its prerequisite

tasks have been done

 There are usually multiple legal topological sorts for a given

DAG

slide-33
SLIDE 33

 Give a topological sort for the following DAG:  A F I C G K D J E H

A H E J D K G C F I

slide-34
SLIDE 34

 Create list L  Add all nodes with no incoming edges into set S  While S is not empty

  • Remove a node u from S
  • Add u to L
  • For each node v with an edge e from u to v

▪ Remove edge e from the graph ▪ If v has no other incoming edges, add v to S

 If the graph still has edges

  • Print "Error! Graph has a cycle"

 Otherwise

  • Return L
slide-35
SLIDE 35
slide-36
SLIDE 36

 Connectivity  Minimum spanning trees  Shortest paths  Matching on bipartite graphs  Stable marriage

slide-37
SLIDE 37

 Work on Project 3  Finish Assignment 4

  • Due tonight!

 Read 4.3 and 4.4