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

Post on 15-Jan-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

Graphs

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

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

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.

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.

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.

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

Undirected State Graphs

WinningPosition

StartingPosition

Undirected State Graphs

WinningPosition

StartingPosition

12

3

45

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.

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.

Directed Graph or Digraph

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

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.

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.

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.

Simple Directed Graph

D E

C

B

A

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

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.

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.

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.

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)

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

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)

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

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.

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.

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

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?

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.

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.

Depth-First Search

D E

C

B

A

Start at node B.

Depth-First Search

D E

C

B

A

Move to node A

Depth-First Search

E

C

B

A

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

D

Depth-First Search

E

C

B

A

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

D

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.

Breadth-First Search

D E

C

B

A

Start at node B.

Breadth-First Search

D E

C

B

A

Visit node B’s neighbor, node A.

Breadth-First Search

D E

C

B

A

Visit node B’s neighbor, node C.

Breadth-First Search

D E

C

B

A

Visit node B’s neighbor, node D.

Breadth-First Search

D E

C

B

A

Visit node C’s neighbor, node E.

Graph Traversals

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

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

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.

top related