1 / 8

Graphs: Definitions

Graphs: Definitions. How would you represent the following? A chart of driving distances between cities of the US A network The prerequisite relationship between CS classes A map for the game “Hunt the Wumpus” These problems have similar structure:

tonyblair
Download Presentation

Graphs: Definitions

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. Graphs: Definitions • How would you represent the following? • A chart of driving distances between cities of the US • A network • The prerequisite relationship between CS classes • A map for the game “Hunt the Wumpus” • These problems have similar structure: • A collection of objects (cities, computers, classes, caves) connected in some way (roads, cables, precedence relationship, doorways)

  2. Graphs: Definitions • Draw a picture where these objects are nodes (vertices) and the connections are links (edges) between vertices. • This picture is called a graph. • Formally: A graph G=(V,E) consists of a set V whose members are called vertices and a set E whose members are called edges and are pairs of distinct vertices from V.

  3. Graphs: Definitions • Adjacent vertices = connected by an edge • An edge (u,v) is incident to vertices u and v • Degree of a vertex = number of edges incident to it (or, number of adjacent vertices) • Path of length k = a sequence (v0, v1, ...vk) such that (vi-1, vi)E • A vertex is reachable from another if there is a path between them • Weighted graph : each edge has an associated weight

  4. Graphs: Examples w complete graph of 5 vertices, aka K5 u v vertices u and v are adjacent (u,v,w,u) is a cycle v a graph with two connected components u in this graph, u is not reachable from v

  5. A[vi,vj]= Graphs: Representation • Adjacency matrix : a |V||V| table A, such that 1, if (vi,vj)E 0, otherwise v1 v2 v3 v4 v5 v1 0 1 1 1 1 v2 1 0 1 0 0 v3 1 1 0 1 0 v4 1 0 1 0 1 v5 1 0 0 1 0 v1 v2 v3 v5 v4

  6. Graphs: Representation • Adjacency list : an array A of |V| lists, one for each vertex. For each vV, A[v] contains all the vertices adjacent to v. v1 v2 v1 v2 v3 v4 v5 v2 v3 v4 v5 v1 v3 v2 v1 v4 v3 v5 v4 v1 v3 v5 v1 v4

  7. Graphs: Representation • What representation would you use for a sparse graph? (i.e. one that has very few edges) • How about a dense graph? • How would you modify the adjacency list/matrix definitions for weighted graphs? • How long does it take to perform the following operations in each representation? • compute the degree of a vertex • insert/delete a vertex • insert/delete an edge • areAdjacent(u,v) • How much space does each representation require?

  8. Graphs: Traversal • Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way • Example: Given a highway map, find a path from one city to another. • What if the graph has cycles? • To avoid getting “trapped” in a cycle, mark the vertices that have already been visited.

More Related