graphs graphs are the most general data structures we will study in this course. a graph is a more...

42
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected node than the tree. Both a graph and a tree are nonlinear data structur consisting of nodes and links between the node. Trees, however, have order within the nodes and gra remove this requirement.

Upload: jack-harrington

Post on 15-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graphs

Graphs are the most general data structures we willstudy in this course.

A graph is a more general version of connected nodesthan the tree.

Both a graph and a tree are nonlinear data structuresconsisting of nodes and links between the node.

Trees, however, have order within the nodes and graphsremove this requirement.

Page 2: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graphs

The simplest form of the graph is called an: undirected graph

Page 3: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graphs

We call a node a vertex and the link an edge or arc.

So the following graph has vertices: V0 - V4

and edges: e0 - e5

V1

V0

V3 V4

V2

e0 e1

e2

e3

e4

e5

Page 4: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graphs

The only important information contained in this graph is the connection between nodes. How they are placed relative to each other is unimportant.

V1

V0

V3 V4

V2

e0 e1

e2

e3

e4

e5

In other words, the appearance of the graph does notmatter. Only the set of vertices and edges is necessary to define a particular graph.

Page 5: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graphs

An undirected graph is a finite set of vertices and connecting edges. Both sets may be empty, yieldingthe empty graph.

Each edge is associated with two vertices and theconnection itself has no order or direction. In other words, the edge that connects u to v and the the edge that connects v to u are the same edge and describe the same graph.

Page 6: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Undirected State Graphs

Graphs are used in problem solving. If a problem can berepresented as a graph, then solving the problem on thecorresponding graph obtains a solution to the orginalproblem.

Each node represents a particular state of the problemand each edge represents a transition from one state to another.

Since the rules determine what constitutes a legal move,only states that can be obtained from the current state areconnected to that node.

Page 7: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Undirected State Graphs

WinningPosition

StartingPosition

•You may flip the middle shape whenever you want.

•You may flip either end if the other two shapes arethe same.

Solve this problem

Page 8: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Undirected State Graphs

WinningPosition

StartingPosition

Page 9: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Undirected State Graphs

WinningPosition

StartingPosition

12

3

45

Page 10: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Undirected State Graphs

WinningPosition

StartingPosition

12

3

45

The graph represent the possible changes of state from oneposition to another. If a path can be found, then the path represents a solution.

Page 11: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Directed Graphs

Sometimes the graph may want to represent direction in additionto the connection. We call such a graph a Directed Graph orDigraph.

In a Digraph the nodes are connected with edges that have thisdirectional property. Nodes are now considered source nodes ifand edge is leaving from them and target (or sink) nodes if theedge is entering them.

Instead of a line to represent an edge, we use an arrow.

Page 12: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Directed Graph or Digraph

Page 13: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Directed State Graph

A directed state graph might be used to represent a gamewhere reversing a move was not allowed. For example, thegame of tic-tac-toe might have a state graph where onenode represents the current board position and a second noderepresents a legal next move.

X

X

X

XO O

O

Page 14: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graphs

Loops - a loop is an edge that connects a node to itself.

Path - a path is a sequence of vertices, such that adjacent pairsof nodes are connected by edges. In a digraph the connection must go from the source to the target.

Circuit - a circuit is path that starts and ends at the same node.

Multiple Edges - a two or more edges connecting the same pairof nodes in the same direction.

Simple Graph - a graph with no loops or multiple edges.

Page 15: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Simple Graph

D E

C

B

A

A digraph may be used to represent airline flights. Each noderepresents a city and each path a scheduled flight.

Page 16: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Simple Directed Graph

D E

C

B

A

For example, there is a flight from city A to city B and a flightfrom city B to city C, etc.

Page 17: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Simple Directed Graph

D E

C

B

A

There are one or more paths from city B to city E.

Page 18: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Simple Directed Graph

D E

C

B

A

A path from city B to city E exists by following the edge from city B to city C and then the edge from city C to city E.

Page 19: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Simple Directed Graph

D E

C

B

A

A path from city B to city E exists by following the edge from city B to city D and then the edge from city D to city E.

Page 20: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Representing the Graph ADT

Adjacency matrix - is a square grid of True/False values thatrepresent the edges of a graph. A graph with n nodes has n rowsand n columns

Linked list - a graph with n nodes can be represented with n different linked lists. List I provides the connections to vertex I.

Edge Set - an array of sets where each set, I, contains the vertexnumbers for all nodes node I is connected to.

Page 21: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

The Graph ADT

Representing problem states as graphs provides a powerfulproblem solving tool.

So far though, we have only expressed our problems as Abstractions. We saw pictures of how things relate to eachother.

We saw this same design model in all the ADTs we studiedto date.

First describe the behavior of the object. (member functions)

Second choose a concrete representation for the object. (data)

Page 22: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Adjacency Matrix

D E

C

B

A

ABCDE

A B C D E

0 1 2 3 40 F T F F F1 T F T T F2 F F F F T3 F F F F T4 T F F F F

Page 23: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Adjacency Matrix

D E

C

B

A

ABCDE

A B C D E

0 1 2 3 40 F T F F F1 T F T T F2 F F F F T3 F F F F T4 T F F F F

Counting the “Trues” on a row indicated how many edges originate from that node. (out degree)Counting the “Trues” in a column indicates the number of edges entering that node. (in degree)

Page 24: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Linked List Representation

D E

C

B

A

Counting the nodes in particular list indicated how many edgesoriginate from that node. (out degree)There is no simple analogy to indicates the number of edgesentering that node. (in degree)

A

B

C

D

E

B

A C D

E

A

E

Page 25: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Adjacency Matrix

ABCDE

A B C D E

0 1 2 3 40 F T F F F1 T F T T F2 F F F F T3 F F F F T4 T F F F F

In general, if space is available, theadjacency matrix is the easier to usethan an edge list or an edge set.

Notice that the space required is greatrelative to the useful information.

Only the “Trues” are necessary. Sincemost of the elements do not contributenecessary information, we call thismatrix a sparse matrix.

Page 26: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Weighed Graph

A weighed graph is a graph where the edges contain values, representing costs.

The weight of a path is the total sum of all the weights of allthe edges in a path.

The shortest path is the one that has the smallest weight.

Page 27: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Weighed Graph

ABCDE

A B C D E

0 1 2 3 40 0 3 0 0 01 7 0 9 2 02 0 0 0 0 13 0 0 0 0 44 5 0 0 0 0

Replacing T/F with the value of the edge’s weigh (cost) transformsan adjacency matrix into a weighed adjacency matrix.

D E

C

B

A

37

9

2

4

1

5

Page 28: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graph Traversals

Depth-First Search - repeats the following pattern

From the starting vertex, v, move along a directed edge to one ofvertex v’s neighbors. From that neighbor, move along a directededge to one of its neighbors.

A depth-first search always goes as far as possible down a particular path before it backs up.

Does this suggest a recursive solution?

Page 29: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graph Traversals

Breadth-First Search - uses a queue to keep track of whichvertices might still have unprocessed neighbors. All neighborsare processed first. This is moving through the graph based onthe distance from the starting node.

Start at the starting vertex. Mark it processed and place it in a queue.

Repeat until queue becomes empty• Remove a vertex, v, from the front of the queue.• For each unmarked neighbor, u of v, mark u processed

and place it in a queue.

Page 30: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graph Traversals

Both the Depth-First Search and the Breadth-First Searchprocess only those nodes where a path exists from the startingnode.

Some graphs, then, contain nodes which will never be processedby either of these two search algorithms.

Both will visit the same nodes, however the order in which theyare processed will differ.

Let’s look at an example of each search order.

Page 31: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Depth-First Search

D E

C

B

A

Start at node B.

Page 32: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Depth-First Search

D E

C

B

A

Move to node A

Page 33: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Depth-First Search

E

C

B

A

Node A has reachable neighbors, go back to B and visit node C.

D

Page 34: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Depth-First Search

E

C

B

A

Node C has a neighbor, node E, so visit it.

D

Page 35: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Depth-First Search

E

C

B

A

D

Node E has no reachable neighbors, not already visited.Return to node B and visit node D.

Page 36: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Breadth-First Search

D E

C

B

A

Start at node B.

Page 37: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Breadth-First Search

D E

C

B

A

Visit node B’s neighbor, node A.

Page 38: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Breadth-First Search

D E

C

B

A

Visit node B’s neighbor, node C.

Page 39: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Breadth-First Search

D E

C

B

A

Visit node B’s neighbor, node D.

Page 40: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Breadth-First Search

D E

C

B

A

Visit node C’s neighbor, node E.

Page 41: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graph Traversals

Depth-First Search - B, A, C, E, D

Breadth-First Search - B, A, C, D, E

Page 42: Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both

Graph - Conclusion

Your text contains working code to implement several of the concepts and behaviors described in this presentation.

Hopefully, you now have a good introductionto graphs and their algorithms.