Title: Chapter 7: Graphs
1Chapter 7 Graphs
2Introduction
- In nonlinear data structure, each element may
have several next elements, which introduces
the concept of branching structures
3Graph
- A graph is a set of points and a set of lines,
which each line joining one point to another. - The points are called nodes
- Lines are called edges
- A set of nodes in the graph G
- VG a,b,c,d
- A set of edges in the graph G
- EG 1,2,3,4,5,6,7,8
- The number of elements in VG is called the order
of graph G. - A null graph is a graph with order zero
4Graph
- An edge is determined by the node it connects.
- Edge 4, for example, connects nodes c and d and
is said to be of form (c,d). - A graph is completely determined by its set of
nodes and set of edges. - The actual positioning of these elements on the
page is unimportant. - So the above two graphs are equivalent.
5Graph
- There may be multiple edges connecting two nodes,
e.g. edge 5, 6 and 7 all of form (b,d). - Some pairs of nodes may not be connected for
example, there is no edge to form (a,c) or (a,d).
- Some edges may connect one node with itself e.g.
edge 8 is of form (a,a). These are called
loops.
6Graph
- Simple graph is a graph G where two of the
following conditions are true - It has no loop, that is, there does not exist an
edge in EG of form (V,V) where V is in VG. - No more than one edge joins any pair of nodes,
that is, there does not exist more than one edge
in EG of form (V1, V2) for any pair of elements
V1, and V2 in VG
d
7Graph
- Multigraph is a graph that is not a simple graph.
- Edges are sometime referred to as arcs
- Nodes are sometime termed vertices
- A connected graph is a graph that cannot be
partitioned into two graphs without removing at
least one edge. - Example of unconnected graph
8Paths
- A path in a graph is a sequence of one or more
edges that connects two nodes. - The length of a path is the number of edges that
it comprises. - The followings are path between nodes b and d.
- P(b,d) (b,c) (c,d) length 2
- P(b,d) (b,c) (c,b) (b,c) (c,d) length 4
- P(b,d) (b,d) length 1
- P(b,d) (b,c) (c,b) (c,d) length 3
- In general, we are interested only in paths in
which a given node is visited no more than once.
This restricts out interest to only the first and
third paths above. The second path visits both
node b and c twice the fourth path visits node b
twice. - We will always be interested in not traversing
the same edge more than once in a path.
9Cycles
- A cycle is a path in which both of the following
conditions are true - No edge appears more than once in the sequence of
edges. - The initial node of the path is the same as the
terminal node of the path i.e. P(V,V). - A cycle returns to where it started.
- Examples
- P(a,a) (a,a)
- P(b,b) (b,c) (c,b)
- P(b,b) (b,c) (c,d) (d,b)
- P(d,d) (d,b) (b,d)
- P(d,d) (d,b) (b,c) (c,d)
10Cycles
- A graph with no cycles is said to be acyclic.
11Directed Graphs
- Another special case of the general graph data
structure is a directed graph, in which
directionality is assigned to the graphs edges. - Each edge of a directed graph includes an arrow.
- The in-degree of a node in a directed graph is
the number of edges that terminate at that node - The out-degree of a node is the number of edges
that emanates from that node. - The degree of a node is the sum of its in- and
out-degrees.
12Directed Graph
- In-degree(a) 1 out-degree(a) 2 degree(a)
3 - In-degree(b) 4 out-degree(b) 2 degree(b)
6 - In-degree(c) 1 out-degree(c) 2 degree(c)
3 - In-degree(d) ? out-degree(d) ? degree(d)
?
13Graphs in Programs
- As with some of the other data structures, the
common programming languages do not have a
built-in data type called graph. - Instead, the graphs characteristics are
simulated by housing it in another data
structure. - There are three major approaches to representing
graph - Matrix representation
- List representation
- Multilist representation
14Adjacency Matrix Representation
- One approach to representing this graph is to use
an adjacency matrix, which is a N-by-N array A. - If there is an edge connecting nodes i and j,
then A(i,j) 1. The adjacency matrix for this
undirected graph is
15Adjacency Matrix Representation
- An edge of a directed graph has its source in one
node and terminates in another node. The
adjacency matrix for this directed graph is
16Adjacency Matrix Representation
- The following is example of graph with weighted
edges. - The nodes represent cities and the edges
represent truck routes between cities. Each edge
is labeled with the distance between the
connected pair of cities.
- Weighted edges are used frequently.
- In transportation applications, the weights
represent distances. - In flow applications, the weights represent
capacities. For example, the nodes on a graph
might represent the gallon/minute capacity of a
pipe-line between the connected location. - In other applications, the edge weights represent
time. For instance, graphs can be used to
represent network of activities.
17Adjacency Matrix Representation
Weighted-edge matrix representation
18Linked representations
- In contrast to the Matrix Representation
Technique, which stores information about every
possible edge. The Linked Representations store
information about only those edges that exist. - As edges are added or deleted from the graph, the
linked representation must be modified
accordingly. - These are two fundamental types of linked
structures to represent graphs - Node directory representation
- Multi-list representation.
19Node Directory Representation
- The node directory representation includes two
parts a directory and a set of linked list. - There is one entry in the directory for each node
of the graph. The directory entry for node I
points to a linked list that represents the nodes
that are connected to node i. - Each record of the linked list has two fields
- One is a node identifier
- One is a link to the next element on the list.
- The directory represents nodes the linked list
represents edges.
20Node Directory Representation
A node directory representation of the undirected
graph
- An undirected graph of order N with E edges
requires N entries in the directory and 2E
linked list entries, except that each loop
reduces the number of linked list entries by one.
21Node Directory Representation
A node directory representation of the directed
graph
- An directed graph of order N with E edges
requires N entries in the directory and E linked
list entries.
22Node Directory Representation
- In the following directed activity graph, each
node represents an event and each edge represents
a task whose completion helps to trigger the next
event, which is the start of other tasks. Each
edges weight is its required time.
Directory
Edge Information
23Multi-list Representation
- In the multi-list representation of graph
structure, there are two parts - A directory of node information
- A set of linked lists of edge information
- There is one entry in the node directory for each
node of the graph. - The directory entry for node i points to a linked
adjacency list of node i. - Each record of the linked list area appears on
two adjacency lists one for the node at each end
of the represented edge.
24Multi-list Representation
Set of edges (1,2),(2,2),(2,3),(4,3),(3,5),(3,6)
25Multi-list Representation
Set of edges (2,1),(2,2),(2,3),(3,4),(5,3),(3,6)
26Graph Traversal
- In many applications it is necessary to visit all
the nodes of a graph. - The two basic graph traversal techniques are
- Breadth-first traversal
- Depth-first traversal
- It is usually necessary to take precautions to
visit each node and edge only once.
27Breadth-first Traversal
- In breadth-first traversal of a graph, one node
is selected as the start position. It is visited
and marked, then all unvisited nodes adjacent to
that nodes are visited and marked in some
sequential order. Finally, the unvisited nodes
immediately adjacent to these nodes are visited
and marked, and so forth, until the entire graph
has been traversed.
- Breadth-first traversal results in visiting the
nodes in the following order 1,2,3,4,5,6,7,8. - The sequence 1,3,2,6,5,4,7,8 is also a valid
breadth-first traversal visitation order.
28Breadth-first Traversal
29Depth-first Traversal
- Whereas breadth-first traversal of a graph
proceeds level-by-level, depth-first traversal
follows first a path from the starting node to an
ending node, then another path from the start to
an end, and so forth until all nodes have been
visited. - Depth-first traversal results in visiting the
nodes in the following order 1,2,4,8,5,7,3,6. - An equally valid sequence to result from
depth-first traversal is 1,3,6,7,8,2,5,4.
30Depth-first Traversal
31Critical Paths
- An event is critical if slippage of its scheduled
completion time causes the completion of the
entire project to be delayed. - The shortest possible completion time for the
project is the longest path through the graph. - The longest path is called the critical path the
critical events lie along this path.
32Critical Paths
- The earliest start time of event i, ESTi, is the
earliest possible trigger time for the event. - The ESTi is calculated from the longest path to
node i from the start node. - ESTi maxESTj T(j,i)
- The latest start time of event i, LSTi, is the
latest possible time that event i can be
triggered with the graph completing in critical
path time. - The set of LSTi are calculated backward from the
last event. - LSTi minLSTj - T(i,j)
33Critical Paths
- The difference between the LSTi and ESTi is the
allowable slippage of event i. - The events on critical path are 1,3,6,7,8, which
are called critical events. - These are the events that need to be controlled
in order for the project to complete on time.
34Minimal Spanning Trees
- A minimal spanning tree is a tree that contains
all the nodes of graph where the distances
occurred in connecting those nodes are minimum.
Minimum total distance is 5,346