Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 13.

Similar presentations


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

1 Graphs Chapter 13

2 Preview Graphs are an important mathematical concept that have significant applications not only in computer science, but also in many other fields. You can view a graph as a mathematical construct, a data structure, or an abstract data type. This chapter provides an introduction to graphs and it presents the major operations and applications of graphs that are relevant to the computer scientist CS 302 Chapter Graphs

3 Terminology Def: A graph G consists of two sets Def: Adjacent Vertices
Edges Def: Adjacent Two vertices are said to be adjacent if they are joined by an edge. CS 302 Chapter Graphs

4 Def: Subgraph A subgraph consists of a set of a graph’s vertices and a subset of its edges CS 302 Chapter Graphs

5 Def: Path Def: Cycle Def: Connected
A path between two vertices is a sequence of edges that begins at one vertex and ends at another. Simple path: does not pass through the same vertex more than once. Def: Cycle A cycle is a path that begins and ends at the same vertex. Def: Connected A graph is connected if each pair of distinct vertices has a path between them CS 302 Chapter Graphs

6 A graph is complete if each pair of distinct vertices has an edge between them
CS 302 Chapter Graphs

7 Since a graph has a set of edges it cannot have duplicate edges.
However, a multigraph does allow multiple edges A graph also does not allow self-edges (loops) CS 302 Chapter Graphs

8 You can label the edges of a graph
When these labels represent numeric values, the graph is called a weighted graph CS 302 Chapter Graphs

9 All of the previous examples were of undirected graphs, because the edges did not indicate a direction. If there is direction, then the graph is called a directed graph (or digraph) CS 302 Chapter Graphs

10 Graphs as ADTs Basic Functions Create, Destroy, Determine empty
Determine number of vertices and edges Is there an edge between I and J? Insert a vertex, or Insert an edge Delete a vertex, or Delete an Edge CS 302 Chapter Graphs

11 Implementing Graphs Adjacency Matrix
The two most common implementations of a graph are the Adjacency Matrix and the Adjacency List Adjacency Matrix Given a graph G with n vertices Matix is n x n of Booleans Entries – True if an edge exists between I and J (false otherwise). CS 302 Chapter Graphs

12 Example: a directed graph.
CS 302 Chapter Graphs

13 Example: a weighted graph
CS 302 Chapter Graphs

14 An Adjacency List Given a graph G with n vertices N linked lists
The ith list has node for vertex j iff there is an edge between i and j CS 302 Chapter Graphs

15 Example: a directed graph
CS 302 Chapter Graphs

16 Example: a weighted graph
CS 302 Chapter Graphs

17 Which implementation is better?
Let’s look at the two most common operations Determine if there is an edge (i, j) Find all vertices adjacent to a given vertex i. The adjacency matrix supports the first one somewhat more efficiently than the list The second operation is supported more efficiently by the adjacency list than the matrix. CS 302 Chapter Graphs

18 Now consider the space requirements
CS 302 Chapter Graphs

19 Graph Traversals Visit all the vertices in the graph.
There are two major ways to do this: Depth first search Breadth first search CS 302 Chapter Graphs

20 Example: the order in which the nodes of a tree are visited in a depth-first search and a breadth-first search. CS 302 Chapter Graphs

21 How do you implement them?
Depth-First Search Typically done with a stack. Breadth-First Search Typically done with a Queue CS 302 Chapter Graphs

22 Example: DFS Start with Node a Trace the Search CS 302
Chapter Graphs

23 Example: DFS CS 302 Chapter Graphs

24 Example: BFS Start with Node a Trace the Search CS 302
Chapter Graphs

25 Example: BFS CS 302 Chapter Graphs

26 Applications of Graphs
Topological Sorting Given the following graph, place the vertices in order so that the edges all point in one direction CS 302 Chapter Graphs

27 Multiple solutions: Applications: Prerequisites ( 201,202,236,336,308,…) CS 302 Chapter Graphs

28 Spanning Trees Given a graph G, construct a tree that has all the vertices of G CS 302 Chapter Graphs

29 A few observations about undirected graphs
A connected undirected graph that has n vertices must have at least n-1 edges A connected undirected graph that has n vertices and exactly n-1 edges cannot contain a cycle. A connected undirected graph that has n vertices and more than n-1 edges must contain at least 1 cycle. CS 302 Chapter Graphs

30 The DFS Spanning Tree CS 302 Chapter Graphs

31 The BFS spanning Tree CS 302 Chapter Graphs

32 Minimum Spanning Trees
Given a weighted undirected graph, compute the spanning tree with the minimum cost CS 302 Chapter Graphs

33 Prim’s Algorithm Start with two sets
Vertices not visited (not part of the tree) Vertices visited (part of the tree – initialized to some start vertex) Find the shortest edge from the visited set to the not visited set. Add the edge to the MST Move the vertex on the other end from not visited to visited. Continue until all vertices are visited. CS 302 Chapter Graphs

34 Trace of Prim’s CS 302 Chapter Graphs

35 CS 302 Chapter Graphs

36 CS 302 Chapter Graphs

37 Shortest Paths Consider once again a map of airline routes.
The shortest path from 0 to 1 in the following graph is not the edge 0-1 (weight 8) but the path ( ) CS 302 Chapter Graphs

38 The algorithm to calculate the answer is attributed to Dijkstra
Need two arrays Weight – Weight[v] = weight of the shortest path from 0 to v Matrix – Matrix[v][u] = weight of edge from v to u Then compare the Weight[u] with Weight[v]+Matrix[v][u]. Put the smallest in Weight[u] Then we need a set of vertices we have visited CS 302 Chapter Graphs

39 We begin with the vertex set initialized to 0
CS 302 Chapter Graphs

40 CS 302 Chapter Graphs

41 CS 302 Chapter Graphs

42 Circuits Eulerian Circuits
Probably the first application of graphs occurred in the early 1700s when Euler proposed the Königsberg bridge problem. Can you start and end at the same point and only cross each bridge once? CS 302 Chapter Graphs

43 Some Difficult Problems
The Traveling Salesperson Problem Def: A Hamiltonian circuit – pass through every vertex once. Given a weighted graph an origin city Problem: the salesperson must begin at an origin city and visit all others (exactly once) and return to the origin and do it with the least cost. CS 302 Chapter Graphs

44 The three utilities problem
Given A graph (3 houses, 3 utilities) Problem Connect each house with each utility so the edges do not cross This is a planarity question (minimum crossing number = 0) for K3,3 CS 302 Chapter Graphs

45 The four color problem Given Problem
A planar graph Problem Can you color the vertices so no two adjacent vertices have the same color? The question was posed more than a century ago (1852) and was only solved in the 1976 (with the help of a computer) Trivia: Nature July 17, 1879 carried a proof of this problem (which was proved false in 1890) CS 302 Chapter Graphs

46 Summary Implementations: Graph searching Trees Problems looked at
Adjacency Matrix and Adjacency List Graph searching uses stacks and Queues Trees Connected, undirected, acyclic graphs Problems looked at Shortest Paths, Eulerian circuits, Hamiltonian Circuits CS 302 Chapter Graphs


Download ppt "Graphs Chapter 13."

Similar presentations


Ads by Google