graphs - hiram collegecs.hiram.edu/~walkerel/cs201/graph.pdfgraphs and trees! • every tree is a...

27
Graphs Ellen Walker CPSC 201 Data Structures Hiram College

Upload: others

Post on 28-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graphs!

Ellen Walker!CPSC 201 Data Structures!

Hiram College!

Page 2: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Basic Graph Terminology!

A C

D B

E

A C

D B

E

Vertex (node)

Edge (link)

cycle

Directed Graph

No cycle

Undirected Graph V = {A,B,C,D,E} E = {{A,B}, {A,C}, {A,D}, {A,E}, {D, E}}

V = {A,B,C,D,E} E = {(A,B), (A,E), (A,D), (D,E), (A,C).(C,A)}

cycle

Path

Page 3: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

More Terminology!

A C

D B

E

•  A is adjacent to B, C, D and E (degree is 4)!

•  D is adjacent to A and E ( degree of D is 2)!

•  There are 2 connected components (ABCDE and FG)!

•  Path BAE is a simple path ; Path BABAC is not.!

•  Path ADEA is a cycle!

F

G

Page 4: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graph Application Example: Prerequisites!

CPSC 171

CPSC 172

CPSC 201

CPSC 240

CPSC 400

CPSC 401

CPSC 331 CPSC 361 CPSC 386

CPSC 152

Page 5: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graph Application Example: Travel Planning!

CLE

ORD

SFO

SJC CLT

PHL DEN

CVG

1

1.5

2

1.5

4

4

2

2.5

2

4

4 4

1

Page 6: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graphs and Trees!

•  Every tree is a graph!–  It is connected!–  It has no cycles!–  Either all links are directed “downward” away from

the root, or it is undirected!•  Every connected, no-cycle, undirected graph

can be a tree!–  Any node can be chosen as the root!

Page 7: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graph ADT !!

•  A set of methods!–  Create a new graph (specify # vertices)!–  Iterate through vertices of graph!–  Iterate through neighbors of a vertex!–  Is there an edge between 2 vertices?!–  What is the weight of an edge between 2 vertices?!–  Insert an edge into the graph!

•  Other options might be available!–  E.g. add vertices, connect a vertex to another, etc.!

Page 8: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Representing Vertices and Edges!

•  Vertex represented as integer!–  Doesnʼt allow named vertices!–  Index into an array of names if you need them!

•  Edge represented by a class!–  Source vertex, destination vertex, weight (default

1.0)!–  For undirected graph, add each edge in both

directions!

Page 9: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graph Representations!

•  Adjacency List!–  Each vertex is associated with a list of edges!–  Looks a lot like a hash table; linked lists hanging

off an array!•  Adjacency Matrix!

–  2D matrix: M[R][C] = weight of edge from R to C!

Page 10: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Adjacency List Example (Directed)!

0 2

3 1

4

0!1!2!3!4!

1 2 3 4

0 4

Page 11: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Adjacency List Example (Undirected)!

0 2

3 1

4

0!1!2!3!4!

1 2 3 4

0 0

0

0 4 3

Page 12: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Adjacency Matrix Example!

0! 1! 2! 3! 4!

0! 5! 8! 7! 7!1! x! x! x! x!2! 10! x! x! x!3! x! x! x! 5!

4! x! x! x! x!

0 2

3 1

4

10 8

7

5 7

5

Value at row,col = weight from row to col The value “x” means infinity (Double.POSITIVE_INFINITY in Java) Unweighted graph can use boolean: (edge = true)

Page 13: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Graph Traversals vs. Tree Traversals!

•  Tree has no cycles; graph can have cycles!–  Mark visited nodes, and donʼt repeat them! !

•  Tree is connected; graph does not have to be!–  After your traversal is “finished” check to see if there are

leftover (not visited) nodes!•  A graph traversal induces a tree structure on the

graph!–  The edges that are used to “find” nodes are part of the tree!–  The root of the tree is the node that started the traversal!

Page 14: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Breadth-First vs. Depth-First!

•  Breadth-first!–  Visit all my immediate neighbors before visiting

their neighbors!–  In tree - all children before any grandchildren

(level-order traversal)!•  Depth-first!

–  Visit first child, first grandchild, … as deep as possible before looking at second child

Page 15: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Breadth First Algorithm!

•  For each vertex!–  If the vertex is unseen, offer it to the queue and

mark it identified!–  While the queue is not empty!

•  Current_vertex = q.poll();!•  For each neighbor of the current vertex!

–  If the neighbor is not yet visited, offer it to the queue and mark it identified!

•  Mark current vertex as visited!•  End for!

–  End While!

Page 16: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Three Kinds of Nodes!

•  Unseen (white in your textbook)!–  Nodes that have not yet been seen at all!

•  Identified (pale blue in your textbook)!–  Nodes that are currently in the queue!–  Must have at least one visited neighbor!

•  Visited (dark blue in your textbook)!–  Nodes that have been completed!–  All neighbors are visited or identified!

Page 17: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Breadth-First Search example!

Page 18: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Depth First Search Algorithm!

•  For each vertex!–  If the vertex is unseen, push it on the stack and

mark it identified!–  While the stack is not empty!

•  Current_vertex = q.pop();!•  For each neighbor of the current vertex!

–  If the neighbor is not yet visited, push it on the stack and mark it identified!

•  Mark current vertex as visited!•  End for!

–  End While!

Page 19: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Depth First Search Example!

Page 20: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Recursive Depth First Search!

•  Rec_dfs(current-node){!•  mark current-node identified!•  for each neighbor of current_node!•  if neighbor is not identified!•  (set parent of neighbor to current node)!•  rec_dfs(neighbor)!•  mark current-node visited!•  }!

Page 21: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Comments on Recursive DFS!

•  Recursive DFS visits the same nodes, in the same order as Stack DFS!

•  Recursive DFS actually uses a stack!•  (Why?)!•  Why is there no recursive BFS?!

Page 22: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

DFS / BFS analysis!

•  Each search visits every node, and considers every neighbor of every node!–  The number of nodes visited is O(V)!–  The number of neigbors considered is O(E)!

•  Every vertex is a neighbor for one of its edges!–  Time proportional to V+E!

•  But, since E >> V (there cannot be fewer than O(V) edges, but there can be O(V2 )), overall time is O(E) in both cases!

Page 23: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Common Graph Algorithms!

•  Dijkstraʼs algorithm!–  Shortest path from one vertex to all others in a

weighted graph!–  If the graph isnʼt weighted use BFS. The path you

take to the node is by definition the shortest.!•  Primʼs algorithm!

–  Finding the spanning tree with the minimum total weight!

–  A spanning tree is a subset of (V-1) edges that minimally connects the graph!

Page 24: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Dijkstraʼs Algorithm!

•  The main idea:!–  If the (best possible known) cost from point A to

point B is x!–  And there is a connection from B to C that costs y,!–  Then the (best possible known) path from point A

to point C through B costs x+y!

Page 25: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Keeping Track of Paths!

•  We will find all possible distances from a starting node, A!

•  Create a table of all other nodes (e.g. B,C,D)!•  Initially, distances are all infinite

(unreachable)!•  Now, weʼll visit one node at a time (starting

with A) and adding all its links to the table…!

Page 26: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Visiting a node!

•  Assume you are looking at node A!–  Suppose there is an edge from A to B costing k!–  Let d(A) and d(B) be the distances (in the table)

from the starting node to A and B respectively!–  If d(A)+k < d(B), change d(B) to d(A)+k and mark

Bʼs predecessor as A!•  Do the above for all edges of A!

Page 27: Graphs - Hiram Collegecs.hiram.edu/~walkerel/cs201/Graph.pdfGraphs and Trees! • Every tree is a graph! – It is connected! – It has no cycles! – Either all links are directed

Putting it all together!

•  Initialize the table!•  Visit the starting node!•  While (not all nodes have been visited)!

–  Visit the node that is closest to the starting point!•  When done, the table will contain all shortest

distances !