Chapter 7: Graphs - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Chapter 7: Graphs

Description:

Adjacency Matrix Representation ... Node Directory Representation. The node directory representation includes two parts: a directory and a set of linked list. ... – PowerPoint PPT presentation

Number of Views:469
Avg rating:3.0/5.0
Slides: 35
Provided by: Dew
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Graphs


1
Chapter 7 Graphs
2
Introduction
  • In nonlinear data structure, each element may
    have several next elements, which introduces
    the concept of branching structures

3
Graph
  • 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

4
Graph
  • 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.

5
Graph
  • 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.

6
Graph
  • 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
7
Graph
  • 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

8
Paths
  • 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.

9
Cycles
  • 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)

10
Cycles
  • A graph with no cycles is said to be acyclic.

11
Directed 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.

12
Directed 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)
    ?

13
Graphs 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

14
Adjacency 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

15
Adjacency 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

16
Adjacency 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.

17
Adjacency Matrix Representation
Weighted-edge matrix representation
18
Linked 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.

19
Node 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.

20
Node 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.

21
Node 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.

22
Node 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
23
Multi-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.

24
Multi-list Representation
Set of edges (1,2),(2,2),(2,3),(4,3),(3,5),(3,6)
25
Multi-list Representation
Set of edges (2,1),(2,2),(2,3),(3,4),(5,3),(3,6)
26
Graph 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.

27
Breadth-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.

28
Breadth-first Traversal
29
Depth-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.

30
Depth-first Traversal
31
Critical 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.

32
Critical 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)

33
Critical 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.

34
Minimal 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
Write a Comment
User Comments (0)
About PowerShow.com