Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.

Similar presentations


Presentation on theme: "ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks."— Presentation transcript:

1 ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks

2 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 node or vertex or point of the graph edge or arc or line of the graph node/hnútur arc/ör

3 n = 1 n = 2 n = 3 A complete,undirected graph has all possible edges. n = 4 8/25/20093 ALG0183 Algorithms & Data Structures by Dr Andy Brooks An n-vertex, complete, undirected graph has n(n-1)/2 edges. (excluding self-loops)

4 London Tube Map http://www.tfl.gov.uk/assets/downloads/standard-tube-map.gif 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4... graphs are everywhere in the real-world. A vertex represents a tube station and might store the station name. An edge represents a railway route between tube stations.

5 Áfangastaðir Flugfélags Íslands www.flugfelag.is 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5... graphs are everywhere in the real-world. A vertex represents an airport and might store the airport code. An edge represents a flight route between two airports and might store the flight time. But we might need two edges for each flight route because flying with and against the wind should be costed differently and/or because the airport taxes might be different. destinations/ áfangastaðir

6 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 Graph definition http://www.itl.nist.gov/div897/sqg/dads/HTML/graph.html Definition: A set of items connected by edges. Each item is called a vertex or node. Formally, a graph is a set of vertices and a binary relation between vertices, adjacency.edgesvertexnodesetbinary relation Formal Definition: A graph G can be defined as a pair (V,E), where V is a set of vertices, and E is a set of edges between the vertices E ⊆ {(u,v) | u, v ∈ V}. If the graph is undirected, the adjacency relation defined by the edges is symmetric, or E ⊆ {{u,v} | u, v ∈ V} (sets of vertices rather than ordered pairs). If the graph does not allow self-loops, adjacency is irreflexive.symmetricself-loops irreflexive Specialization (... is a kind of me.) directed graph, undirected graph, acyclic graph, directed acyclic graph, planar graph, biconnected graph, connected graph, complete graph, dense graph, sparse graph, hypergraph, multigraph, labeled graph, weighted graph, tree. directed graphundirected graphacyclic graphdirected acyclic graph planar graphbiconnected graphconnected graphcomplete graph dense graphsparse graphhypergraphmultigraphlabeled graph weighted graphtree

7 8/25/20097 A graph G = (V,E) G = (V,E) – V is the vertex set, E is the edge set. – Vertices are also called nodes and points. – Edges are also called arcs and lines. Each edge connects two different vertices. An undirected edge has no orientation (u,v). – edge (u,v) is the same as edge (v,u) A directed edge has an orientation (u,v). – edge (u,v) is not the same as edge (v,u) Undirected graph => no oriented edge. Directed graph (“digraph”) => every edge has an orientation. uv uv ALG0183 Algorithms & Data Structures by Dr Andy Brooks one-way street one-way street/einstefnugata

8 8/25/20098 Some Graphs incomplete & directed Number of edges in (a) <= n(n-1)/2 Number of edges in (c) <= n(n-1) Unconnected. incomplete & undirected incomplete & undirected & unconnected Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks

9 8/25/20099 Some Spanning Trees Connected, undirected subgraphs (no cycles) that include all vertices of the original graph. n vertices, n-1 edges Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks

10 8/25/200910 Street map application e.g. guiding taxi drivers If we associated lengths with each edge, we would have a weighted digraph and we could try to find the shortest way of getting from intersection i to intersection j. Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks

11 8/25/200911 Weighted graph example The weights might represent the cost of upgrading roads or laying optical cables between cities. Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks Question: Is 110 the minimum cost spanning tree? We need to use Kruskal's algorithm.

12 Edges incident on a vertex – a, d, and b are incident on V in (1) and (2) Adjacent vertices: – A vertex u is adjacent (or next) to v if there is an edge from v to u. – In (1), U is adjacent to V, but not vice versa. – In (2), U and V are adjacent to each other Degree of a vertex – X has degree 2 in (1) – X has degree 5 in (2) – X has in-degree 2 in (1) – X has out-degree 0 in (1) Parallel edges – h and i are parallel edges Self-loop – j is a self-loop 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 based on a slide by Tangming Yuan

13 Path – a sequence of vertices connected by edges – each successive vertex is adjacent to its predecessor Simple path – path such that all its vertices in the path are distinct Examples – P1=(V, X, Z) is a simple path – P2=(U, W, X, Y, W, V) is a path that is not simple 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 based on a slide by Tangming Yuan

14 Cycle – a path in which the first and final vertices are the same Simple cycle – cycle such that all its vertices and edges are distinct except the first and final vertices Examples – C1 = (V, X, Y, W, U, V) is a simple cycle – C2 = (U, W, X, Y, W, V, U) is a cycle that is not simple 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 based on a slide by Tangming Yuan A directed graph having no cycles is called a directed acyclic graph (DAG).

15 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 adjacency-matrix representation http://www.itl.nist.gov/div897/sqg/dads/HTML/adjacencyMatrixRep.html Definition: A representation of a directed graph with n vertices using an n × n matrix, where the entry at (i,j) is 1 if there is an edge from vertex i to vertex j; otherwise the entry is 0. A weighted graph may be represented using the weight as the entry. An undirected graph may be represented using the same entry in both (i,j) and (j,i) or using an upper triangular matrix. (Definition: A matrix that is only defined at (i,j) when i ≤ j.)directed graphverticesmatrixedgeweighted graphundirected graphupper triangular matrixmatrix example by Dr Srinivasan Ramaswamy

16 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 adjacency-list representation http://www.itl.nist.gov/div897/sqg/dads/HTML/adjacencyListRep.html Definition: A representation of a directed graph with n vertices using an array of n lists of vertices. List i contains vertex j if there is an edge from vertex i to vertex j. A weighted graph may be represented with a list of vertex/weight pairs. An undirected graph may be represented by having vertex j in the list for vertex i and vertex i in the list for vertex j.directed graphverticesarraylistsedgeweighted graphundirected graph The adjacency-list representation is more compact for a sparse matrix. (Definition: A matrix that has relatively few non-zero (or "interesting") entries. It may be represented in much less than n × m space.)sparse matrix “In most applications, however, a sparse graph is the norm.” Weiss e.g. an airport does not have a direct flight route to every other airport...

17 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 Note: Suppose we have a directed graph with four vertices. Here are adjacency-matrix and adjacency-list representations. The arrow (->) means a link in a list. 1 2 3 4 1 1 1 1 1 2 1 0 0 0 3 0 1 0 1 4 0 1 1 0 1 -> 1 -> 2 -> 3 -> 4 2 -> 1 3 -> 2 -> 4 4 -> 2 -> 3 Big-Oh(space) is|V| 2 When all the edges are present |E| = |V| 2. Big-Oh(space) is |V|+|E| -> |E| Algorithms developed for sparse graphs typically use adjacency-list representations. A graph G = (V,E) list order “unimportant”

18 Figure 14.1 A directed graph Weiss ©Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18 The shortest path between V 0 and V 5 has two edges with a cost of 1+8=9. The weighted shortest path between V 0 and V 5 has three edges and a cost of 1+4+1=6.


Download ppt "ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks."

Similar presentations


Ads by Google