Download presentation
Presentation is loading. Please wait.
Published byYanti Hermanto Modified over 5 years ago
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
Graphs: Representation
Adjacency matrix : a |V||V| table A, such that A[vi,vj]= 1, if (vi,vj)E 0, otherwise v1 v2 v3 v4 v5 v v v v v v1 v2 v3 v5 v4
6
Graphs: Representation
Adjacency list : an array A of |V| lists, one for each vertex. For each vV, 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.