1 / 18

Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs. Objects and Connections. Many problems are naturally formulated in terms of objects and the connections between them. For example: Airline Routes--What is the fastest (or cheapest) way to get from one city to another?

misae
Download Presentation

Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresCSCI 132, Spring 2014Lecture 38Graphs

  2. Objects and Connections • Many problems are naturally formulated in terms of objects and the connections between them. For example: • Airline Routes--What is the fastest (or cheapest) way to get from one city to another? • Electrical circuits--Circuit elements are wired together. How does the current flow? • Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs. • Links between web pages A graph is a mathematical object that describes such situations. Algorithms for graphs are fundamental to CS. Graph Theory is a major branch of combinatorial mathematics.

  3. a d a d b c b c Graphs • A graph is a collection of vertices, V, and edges, E. • An edge connects two vertices. is the same as: Vertices: a, b, c, d Edges: ab, bc, ac, ad

  4. a d b c a d e b c Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. Example: A path from a to c could be ac or abc. A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. Not Connected

  5. a d b c a d b c More definitions A simple path is a path with no vertex repeated. A simplecycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3. Example: abca is a cycle A tree is a graph with no cycles.

  6. a d a d a d b c b c b c Complete Dense Sparse Definitions Continued A complete graph is a graph in which all edges are present. A sparse graph is a graph with relatively few edges. A dense graph is a graph with many edges.

  7. 34 a d a d a d 22 4 13 19 b c b c b c Undirected Directed Weighted Types of Graphs An undirected graph has no specific direction between the vertices. A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa. A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc.

  8. Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 1 2 3 2 3 5 4 4 5 Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

  9. Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 2 5 1 1 2 1 5 4 3 2 3 2 4 3 5 4 2 5 3 4 1 2 4 5 Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

  10. Representing a Graph with an Adjacency Matrix An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex. 1 2 1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 1 3 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 1 0 3 5 4 The diagonal may be zero or one. Each edge is represented twice.

  11. Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 2 4 5 6 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6

  12. Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 4 2 1 5 2 4 5 6 5 6 3 1 2 3 4 5 6 1 0 1 0 1 0 0 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1 2 4 4 5 6 6

  13. Representing Weighted Graphs In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 1 3 6 5 7 2 4 1 4 5 6 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6

  14. Representing Weighted Graphs In weighted graphs we include the weights of each edge. 2 List 1 2 3 4 1 2 2 1 1 3 6 5 7 5 5 2 4 1 4 5 6 5 6 6 7 3 1 2 3 4 5 6 1 0 2 0 1 0 0 2 0 0 0 0 5 0 3 0 0 0 0 6 7 4 0 3 0 0 0 0 5 0 0 0 4 0 0 6 0 0 0 0 0 1 2 3 4 4 4 5 6 6 1

  15. Lists vs. Matrices Speed: An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph. For an adjacency list, there is no faster way than to search the entire list for the presence of the edge. Memory: Normally, an adjacency matrix requires more space (n2 entries). However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.

  16. Breadth First Traversal • Problem: • Given a graph, G = (V, E), find all the vertices reachable from some source, s. • Repeat for all unvisited vertices • Strategy: • Visit all vertices adjacent to s first. • Traversal expands outward level by level. (Visit all vertices a distance of 2 away from s second, then those at distance 3, etc.) • Keep track of nodes that have been visited already (Otherwise may visit a vertex twice or end up in an endless cycle).

  17. Pseudocode for Breadth First Traversal template <intmax_size> void Digraph <max_size>:: breadth_first(void (*visit)(vertex &)) const { Queue q; boolvisited[max_size]; Vertex v, w, x; for (all v in G) visited[v] = false; for (all v in G) if (!visited[v]) { q.append(v); while(!q.empty( )) { q.retrieve(w); if(!visited[w]) { visited[w] = true; (*visit)(w); for (all x adjacent to w) q.append(x); } q.serve( ); } } }

  18. Example a b c d e f g h We will work through this in class.

More Related