Graph • A geometric diagram consisting of a finite number of dots called vertices joined by a finite number of curved or straight line segments called edges.
Definitions (6) • The path length is two edges (the shortest path between V0 and V5) and the weighted path length is 9. • However, if cost important, the weighted shortest path between these vertices has cost 6 and is V0, V3, V6, V5.
Definitions (7) • A path may exist from a vertex to itself. If this path contains no edges, the path length is 0, which is a convenient way to define an otherwise special case. • A simple path is a path in which all vertices are distinct, except that the first and last vertices can be the same.
Definitions (8) • A cycle in a directed graph is a path that begins and ends at the same vertex and contains at least one edge. That is, it has a length of at least 1 such that w1 = wN. • The cycle is simple if the path is simple. • A Directed Acyclic Graph -DAG is a type of directed graph having no cycles.
Definitions (9) • An example of a real-life situation that can be modeled by a graph is an airport system. • Each airport is a vertex. If there is a nonstop flight between two airports, two vertices are connected by an edge. The edge could have a weight representing time, distance, or the cost of the flight.
Definitions (10) • In an undirected graph, an edge (v, w) would imply an edge (w, v). • However, the costs of the edges might be different because flying different directions might take longer (depending on prevailing winds) or cost more (depending on local taxes).
numbered starting from 0. • One simple way to represent a graph is to use a two-dimensional array called an adjacency matrix. http://oneweb.utc.edu/~Christopher-Mawata/petersen/lesson7.htm
Graph Representation (2) • For each edge (v, w), we set a[v] [w] equal to the edge cost. • Nonexistent edges can be initialized with a logical INFINITY. • The initialization of the graph seems to require that the entire adjacency matrix be initialized to INFINITY.
entry is set. In this scenario, the initialization takes | E| ≤|V|2) time. • When most edges are present, we have |E|= Θ|V|2 . • Such a graph is considered to be a dense graph, it has a large number of edges, generally quadratic.
adjacency list, which represents a graph by using linear space. • For each vertex, we keep a list of all adjacent vertices. An adjacency list representation of the graph in Figure 14.1 using a linked list is shown in Figure 14.2.
Adjacency List (3) • Note that when inserting an edge, we do not check whether it is already present. • That cannot be done in constant time (using a simple linked list), and doing the check would destroy the linear-time bound for construction.
Shortest Path Calculation • Figure 14.4 shows the representation in which we use internal numbers. • Figure 14.5 replaces the internal numbers with Vertex variables, as we do in our code.