 # 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.

## Presentation on theme: "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."— Presentation transcript:

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 a graph and a tree are nonlinear data structures consisting of nodes and links between the node. Trees, however, have order within the nodes and graphs remove 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: V 0 - V 4 and edges: e 0 - e 5 V1V1 V0V0 V3V3 V4V4 V2V2 e0e0 e1e1 e2e2 e3e3 e4e4 e5e5

Graphs The only important information contained in this graph is the connection between nodes. How they are placed relative to each other is unimportant. V1V1 V0V0 V3V3 V4V4 V2V2 e0e0 e1e1 e2e2 e3e3 e4e4 e5e5 In other words, the appearance of the graph does not matter. 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, yielding the empty graph. Each edge is associated with two vertices and the connection 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 be represented as a graph, then solving the problem on the corresponding graph obtains a solution to the orginal problem. Each node represents a particular state of the problem and 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 are connected to that node.

Undirected State Graphs Winning Position Starting Position You may flip the middle shape whenever you want. You may flip either end if the other two shapes are the same. Solve this problem

Undirected State Graphs Winning Position Starting Position

Undirected State Graphs Winning Position Starting Position 1 2 3 4 5

Undirected State Graphs Winning Position Starting Position 1 2 3 4 5 The graph represent the possible changes of state from one position 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 addition to the connection. We call such a graph a Directed Graph or Digraph. In a Digraph the nodes are connected with edges that have this directional property. Nodes are now considered source nodes if and edge is leaving from them and target (or sink) nodes if the edge 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 game where reversing a move was not allowed. For example, the game of tic-tac-toe might have a state graph where one node represents the current board position and a second node represents a legal next move. X X X X O 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 pairs of 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 pair of nodes in the same direction. Simple Graph - a graph with no loops or multiple edges.

Simple Graph DE C B A A digraph may be used to represent airline flights. Each node represents a city and each path a scheduled flight.

Simple Directed Graph DE C B A For example, there is a flight from city A to city B and a flight from city B to city C, etc.

Simple Directed Graph DE C B A There are one or more paths from city B to city E.

Simple Directed Graph DE 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 DE 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 that represent the edges of a graph. A graph with n nodes has n rows and 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 vertex numbers for all nodes node I is connected to.

The Graph ADT Representing problem states as graphs provides a powerful problem solving tool. So far though, we have only expressed our problems as Abstractions. We saw pictures of how things relate to each other. We saw this same design model in all the ADTs we studied to date. First describe the behavior of the object. (member functions) Second choose a concrete representation for the object. (data)

Adjacency Matrix DE C B A ABCDEABCDE A B C D E 0 1 2 3 4 0 F T F F F 1 T F T T F 2 F F F F T 3 F F F F T 4 T F F F F

Adjacency Matrix DE C B A ABCDEABCDE A B C D E 0 1 2 3 4 0 F T F F F 1 T F T T F 2 F F F F T 3 F F F F T 4 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 DE C B A Counting the nodes in particular list indicated how many edges originate from that node. (out degree) There is no simple analogy to indicates the number of edges entering that node. (in degree) A B C D E B ACD E A E

Adjacency Matrix ABCDEABCDE A B C D E 0 1 2 3 4 0 F T F F F 1 T F T T F 2 F F F F T 3 F F F F T 4 T F F F F In general, if space is available, the adjacency matrix is the easier to use than an edge list or an edge set. Notice that the space required is great relative to the useful information. Only the “Trues” are necessary. Since most of the elements do not contribute necessary information, we call this matrix 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 all the edges in a path. The shortest path is the one that has the smallest weight.

Weighed Graph ABCDEABCDE A B C D E 0 1 2 3 4 0 0 3 0 0 0 1 7 0 9 2 0 2 0 0 0 0 1 3 0 0 0 0 4 4 5 0 0 0 0 Replacing T/F with the value of the edge’s weigh (cost) transforms an adjacency matrix into a weighed adjacency matrix. DE C B A 3 7 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 of vertex v’s neighbors. From that neighbor, move along a directed edge 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 which vertices might still have unprocessed neighbors. All neighbors are processed first. This is moving through the graph based on the 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 Search process only those nodes where a path exists from the starting node. Some graphs, then, contain nodes which will never be processed by either of these two search algorithms. Both will visit the same nodes, however the order in which they are processed will differ. Let’s look at an example of each search order.

Depth-First Search DE C B A Start at node B.

Depth-First Search DE 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 DE C B A Start at node B.

Breadth-First Search DE C B A Visit node B’s neighbor, node A.

Breadth-First Search DE C B A Visit node B’s neighbor, node C.

Breadth-First Search DE C B A Visit node B’s neighbor, node D.

Breadth-First Search DE 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 introduction to graphs and their algorithms.

Download ppt "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."

Similar presentations