Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs.

Similar presentations


Presentation on theme: "Graphs."— Presentation transcript:

1 Graphs

2 Chapter Outline Graph background and terminology
Data structures for graphs Graph traversal algorithms Minimum spanning tree algorithms Shortest-path algorithm

3 What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of edges describes relationships among the vertices

4 Formal definition of graphs
A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices) V(G) and E(G) represent the sets of vertices and edges of G, respectively

5 Examples for Graph 1 2 1 2 1 3 3 4 5 6 G1 2 G2 G3 complete graph
1 2 1 2 1 3 3 4 5 6 G1 2 G2 complete graph incomplete graph G3 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={(0,1),(1,0),(1,2)}

6 Complete Graph A complete graph is a graph that has the maximum number of edges for undirected graph with n vertices, the maximum number of edges is n(n-1)/2 for directed graph with n vertices, the maximum number of edges is n(n-1) example: G1 is a complete graph

7 Directed vs. undirected graphs
When the edges in a graph have no direction, the graph is called undirected The pairs of vertices representing any edges is unordered e.g., (v0, v1) and (v1, v0) represent the same edge (v0, v1) = (v1,v0)

8 Directed vs. undirected graphs (cont.)
When the edges in a graph have a direction, the graph is called directed (or digraph) - Each edge as a directed pair of vertices Warning: if the graph is directed, the order of the vertices in each edge is important !! (v0, v1) != (v1,v0) E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7), (9,9), (11,1)}

9 Trees vs graphs Trees are special cases of graphs!!

10 Graph terminology Adjacent nodes: two nodes are adjacent if they are connected by an edge Path: a sequence of vertices that connect two nodes in a graph Complete graph: a graph in which every vertex is directly connected to every other vertex

11 Graph terminology (cont.)
What is the number of edges in a complete directed graph with N vertices?  N * (N-1)

12 Graph terminology (cont.)
What is the number of edges in a complete undirected graph with N vertices?  N * (N-1) / 2

13 Graph terminology (cont.)
Weighted graph: a graph in which each edge carries a value

14 Data Structures for Graphs an Adjacency Matrix
A two-dimensional matrix or array that has one row and one column for each node in the graph For each edge of the graph (Vi, Vj), the location of the matrix at row i and column j is 1 All other locations are 0 For an undirected graph, the matrix will be symmetric along the diagonal For a weighted graph, the adjacency matrix would have the weight for edges in the graph

15 Adjacency Matrix Example 1

16 Adjacency Matrix Example 2

17 Data Structures for Graphs An Adjacency List
A list of pointers, one for each node of the graph These pointers are the start of a linked list of nodes that can be reached by one edge of the graph For a weighted graph, this list would also include the weight for each edge

18 Adjacency List Example 1

19 Adjacency List Example 2

20 Graph implementation Array-based implementation
A 1D array is used to represent the vertices A 2D array (adjacency matrix) is used to represent the edges

21 Array-based implementation

22 Graph implementation (cont.)
Linked-list implementation A 1D array is used to represent the vertices A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)

23 Linked-list implementation

24 Adjacency matrix vs. adjacency list representation
Good for dense graphs Memory requirements: O(V2 ) Connectivity between two vertices can be tested quickly Adjacency list Good for sparse graphs Memory requirements: O(V + E)=O(V) Vertices adjacent to another vertex can be found quickly

25 Graph searching Problem: find a path between two nodes of the graph (e.g., Austin and Washington) Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

26 Depth-First-Search (DFS)
What is the idea behind DFS? Travel as far as you can down a path Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) DFS can be implemented efficiently using a stack

27 Depth-First-Search (DFS) (cont.)
depth-first traversal starting at node A: ABFHCDGIE

28 Depth-First-Search (DFS) (cont.)
Pick a starting point— say A. Then do 3 things: visit this vertex, push it onto a stack so you can remember it, and mark it so you won’t visit it again. Next, go to any vertex adjacent to A that hasn’t yet been visited. say B. Visit B, mark it, and push it on the stack. At B, repeat as before: this process is Rule 1. RULE 1 If possible, visit an adjacent unvisited vertex, mark it, and push it on the stack. RULE 2 If you can’t follow Rule 1, then, if possible, pop a vertex off the stack. RULE 3 If you can’t follow Rule 1 or Rule 2, you’re done.

29 Breadth-First-Searching (BFS)
What is the idea behind BFS? Look at all possible paths at the same depth before you go at a deeper level Back up as far as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex)

30 Breadth-First-Searching (BFS) (cont.)
ABCDEFGHI

31 Breadth-First-Searching (BFS) (cont.)
Pick a starting point— say A. RULE 1 Visit the next unvisited vertex (if there is one) that’s adjacent to the current vertex, mark it, and insert it into the queue. RULE 2 If you can’t carry out Rule 1 because there are no more unvisited vertices, remove a vertex from the queue (if possible) and make it the current vertex. RULE 3 If you can’t carry out Rule 2 because the queue is empty, you’re done

32 Breadth-First-Searching (BFS) (cont.)
BFS can be implemented efficiently using a queue Set found to false queue.Enqueue(startVertex) DO queue.Dequeue(vertex) IF vertex == endVertex Set found to true ELSE Enqueue all adjacent vertices onto queue WHILE !queue.IsEmpty() AND !found Should we mark a vertex when it is enqueued or when it is dequeued ? IF(!found) Write "Path does not exist"

33 Breadth-First Traversal
From the starting node, follow all paths of length one Then follow paths of length two that go to unvisited nodes Continue increasing the length of the paths until there are no unvisited nodes along any of the paths

34 DFS and BFS - EXAMPLE depth first search: v0, v1, v3, v7, v4, v5, v2, v6 breadth first search: v0, v1, v2, v3, v4, v5, v6, v7

35 Depth-First Traversal Example
Consider the following graph: The order of the depth-first traversal of this graph starting at node 1 would be: ??

36 Breadth-First Traversal Example
Consider the following graph: The order of the breadth-first traversal of this graph starting at node 1 would be: ??

37 Traversal Analysis If the graph is connected, these methods will visit each node exactly once Because the most complex work is done when the node is visited, we can consider these to be O(N) If we count the number of edges considered, we will find that is also linear with respect to the number of graph edges

38 Minimum Spanning Tree (MST)
A graph with the minimum number of edges necessary to connect the vertices. Figure a) shows 5 vertices with excessive number of edges, while b) shows same vertices with minimum number of edges necessary to connect them. Figure b) constitutes a minimum spanning tree (MST). There are many possible minimum spanning trees for a given set of vertices. Figure b) shows edges {AB, BC, CD, DE}, but edges {AC, CE, ED, DB} form an MST as well.

39 Minimum Spanning Tree (MST)
The number of edges E in an MST is always one less than the number of vertices V: E = V – 1 Remember we’re not trying to find a minimum physical length, just the minimum number of edges. The algorithm for creating the minimum spanning tree is almost identical to the depth-first search or the breadth first search algorithms.

40 Minimum Spanning Tree with Weighted Graphs
The minimum spanning tree (MST) of a weighted connected graph is a subgraph that contains all of the nodes of the original graph and a subset of the edges so that: The subgraph is connected The total weights of the subgraph edges is the smallest possible

41 The Dijkstra-Prim Method
This is a greedy algorithm that solves the larger problem by looking at a subset and making the best choice for it In this case, we will look at all of the edges from the starting node and choose the smallest On each pass, we will choose the smallest of the edges from nodes already in the MST to nodes not in the MST (the fringe)

42 Dijkstra-Prim Example

43 Dijkstra-Prim Example

44 Dijkstra-Prim Example

45 Dijkstra-Prim Example

46 The Dijkstra-Prim Algorithm
Select a starting node build the initial fringe from nodes connected to the starting node while there are nodes left do choose the edge to the fringe of the smallest weight add the associated node to the tree update the fringe by: adding nodes to the fringe connected to the new node updating the edges to the fringe so that they are the smallest end while

47 Shortest-Path Algorithm
The shortest-path algorithm finds the series of edges between two nodes that has the smallest total weight

48 MST vs. Shortest Path The MST algorithm can skip an edge of larger weight but include many edges of smaller weight that results in a longer path than the single edge

49 Shortest-path problem
There are multiple paths from a source vertex to a destination vertex Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm BFS can be used to solve the shortest path problem when the graph is weightless or all the weights are the same

50 Dijkstra’s Method This is similar to the Dijkstra-Prim MST algorithm, but instead of just looking at the single shortest edge from a node to the fringe, we look at the shortest path from the start node to a fringe node

51 Dijkstra’s Algorithm  Select a starting node
 build the initial fringe from nodes connected to the starting node  while we are not at the destination node do choose the fringe node with the shortest path to the starting node add that node and its edge to the tree update the fringe by: adding nodes that are connected to the new node to the fringe for each node in the fringe do update its edge to the one connected to the tree on the shortest path to the starting node end for end while

52 Dijkstra’s Shortest-Path Example
source destination

53 Dijkstra’s Shortest-Path Example

54 Dijkstra’s Shortest-Path Example

55 Dijkstra’s Shortest-Path Example

56 Work out the MST from A Example: Suppose we want to install a cable television line that connects six towns. Figure shows a weighted graph with six vertices, representing the towns. Each edge has a weight, representing the cost, in millions of dollars, of installing a cable link between two cities. Five links will connect the six cities, but which five links should they be?

57

58 Work out minimum fares to all locations from A
This time, we just want to find the cheapest route from one city to another. Note that the edges are directed

59 The Shortest-Path Array
A B C D E (A) 100(D) 80(A) 140(C)

60 Efficiency The issue is complicated by the two ways of representing graphs: the adjacency matrix and adjacency lists. If an adjacency matrix is used, the algorithms mostly require O(V2) time, where V is the number of vertices. For large matrices, O(V2) isn’t very good performance. If the graph is dense, there isn’t much we can do about improving this performance. (By dense we mean a graph that has many edges—one in which many or most of the cells in the adjacency matrix are filled.) However, many graphs are sparse. In a sparse graph, running times can be improved by using the adjacency-list representation rather than the adjacency matrix. - You don’t waste time examining adjacency-matrix cells that don’t hold edges. For unweighted graphs, depth-first search with adjacency lists requires O(V+E) time For weighted graphs, both the minimum spanning tree and the shortest-path algorithm require O((E+V)logV) time. In large sparse graphs, these times can represent dramatic improvements over the adjacency matrix approach. However, the algorithms are somewhat more complicated


Download ppt "Graphs."

Similar presentations


Ads by Google