alg0183 algorithms & data structures lecture 18 the basics of graphs. 8/25/20091 alg0183...
TRANSCRIPT
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
1
ALG0183 Algorithms & Data Structures
Lecture 18The basics of graphs.
8/25/2009
Watch out for self-loops in graphs.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
28/25/2009
node or vertex or point of the graph
edge or arc or line of the graph
some terminologynode/hnúturarc/ör
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
3
n = 1
n = 2 n = 3
some terminology
A complete ,undirected graph has all possible edges.
n = 4
complete
8/25/2009
An n-vertex, complete, undirected graph has n(n-1)/2 edges.
(excluding self-loops)
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
4
London Tube Maphttp://www.tfl.gov.uk/assets/downloads/standard-tube-map.gif
8/25/2009
... 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.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
5
Áfangastaðir Flugfélags Íslands www.flugfelag.is
8/25/2009
... 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
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
68/25/2009
Graph definitionhttp://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.
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.
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.
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
78/25/2009
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.
u v
u v
one-way street
one-way street/einstefnugata
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
88/25/2009
Some Graphs incomplete &directed
Number of edges in (a) <= n(n-1)/2Number of edges in (c) <= n(n-1)
Unconnected.incomplete &undirected
incomplete &undirected & unconnected
Sahni, © McGraw-Hill
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
98/25/2009
Some Spanning Trees Connected, undirected subgraphs (no cycles) that include all vertices of the original graph.
n vertices, n-1 edgesSahni, © McGraw-Hill
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
108/25/2009
Street map applicatione.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
118/25/2009
Weighted graph exampleThe weights might represent the cost of upgrading roads or laying optical cables between cities.
Sahni, © McGraw-Hill
Question: Is 110 the minimum cost spanning tree? We need to use Kruskal's algorithm .
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
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
some terminologybased on a slide by Tangming Yuan
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
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
some terminologybased on a slide by Tangming Yuan
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
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
some terminologybased on a slide by Tangming Yuan
A directed graph having no cycles is called a directed acyclic graph (DAG).
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
158/25/2009
adjacency-matrix representationhttp://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.)
example by Dr Srinivasan Ramaswamy
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
168/25/2009
adjacency-list representationhttp://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.
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.)
“In most applications, however, a sparse graph is the norm.” Weisse.g. an airport does not have a direct flight route to every other airport...
ALG0183 Algorithms & Data Structures by Dr Andy Brooks
178/25/2009
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 41 1 1 1 12 1 0 0 03 0 1 0 14 0 1 1 0
1 -> 1 -> 2 -> 3 -> 42 -> 13 -> 2 -> 44 -> 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)
quadratic
linearlist order “unimportant”