Вы находитесь на странице: 1из 43

CSE 203: Data Structures

Lecture 5: Graphs

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Graphs
A graph G = (V, E) consists of a set of
vertices V, and a set of edges E, such that
each edge in E is a connection between a
pair of vertices in V.
The number of vertices is written |V|, and the
number edges is written |E|.

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Graphs (2)

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Paths and Cycles


Path: A sequence of vertices v1, v2, , vn of length
n-1 with an edge from vi to vi+1 for 1 <= i < n.
A path is simple if all vertices on the path are
distinct.
A cycle is a path of length 3 or more that connects
vi to itself.
A
cycle is simple if Course
the Teacher:
pathProf.
isSohel
simple, except the
Started on 09 April 2016
Rahman

There is a simple path from Vertex 0 to Vertex 3 containing


Vertices 0, 1, and 3.
Vertices 0, 4, 2, 3, 1, 4, 2, 3, 1 also form a path, but not a
simple path because some vertices appear twice.
Vertices 0, 1, 4, and 0 form a simple cycle.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Connected Components
A subgraph S is formed from a graph G = (V,
E) by selecting a subset V_s of V a subset E_s
of E such that for every edge e in E_s, both of
es vertices are in V_s.
An undirected graph is connected if there is at least
one path from any vertex to any other.
The maximum connected subgraphs of an
undirected graph are called connected components.

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Acyclic graphs
A graph without cycles is called
acyclic.
Thus, a directed graph without cycles
is called a directed acyclic graph or
DAG.
A free tree is a connected, undirected
graph with no simple cycles.
An equivalent definition is that a free
tree is connected and has |V|-1 edges.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Directed Representation

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Undirected Representation

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

Comparison
Adjacency List
Stores information only for
those edges that actually
appear in the graph
Needs pointers.

Adjacency Matrix
Requires space for each
potential edge, whether it
exists or not
Requires no overhead for
pointers, which can be a
substantial cost, especially if
the only information stored
for an edge is one bit to
indicate its existence.
Sparse graphs are likely to However, As the graph
have their adjacency list becomes
denser,
the
representation
be
more adjacency matrix becomes
space efficient.
relatively
more
space
Course Teacher: Prof. Sohel
efficient.
Started on 09 April 2016
10
Rahman

Space and Time


Adjacency Matrix: O(|V^2|)
Adjacency List: O(|V| + |E|)

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

11

Graph ADT
We assume that
vertices are defined by an integer index
value.
there is a Vertex 0, Vertex 1, and so on

There is a useful array called Mark

C++3elatestP387.pdf

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

12

Matrix Implementation

C++3elatestP389-390.pdf

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

13

List Implementation

C++3elatestP391-392.pdf

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

14

Graph Traversals
Some applications require visiting every
vertex in the graph exactly once.
The application may require that vertices be
visited in some special order based on
graph topology.
Examples:

Artificial Intelligence Search


Shortest paths problems

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

15

Graph Traversals Difficulties


First, it may not be possible to reach
all vertices from the start vertex.
This occurs when the graph is not
connected

Second, the graph may contain


cycles, and we must make sure that
cycles do not cause the algorithm to
go into an infinite loop.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

16

Graph Traversals Difficulties: Solutions


Graph traversal algorithms can solve both of these
problems by maintaining a mark bit for each vertex
on the graph.
At the beginning of the algorithm, the mark bit for all
vertices is cleared.
The mark bit for a vertex is set when the vertex is first
visited during the traversal.
If a marked vertex is encountered during traversal, it is not
visited a second time. This keeps the program from going
into an infinite loop when it encounters a cycle.
Once the traversal algorithm completes, we can check to
see if all vertices have been processed by checking the
mark bit array.
If not all vertices are marked, we can continue the
traversal from another unmarked vertex.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

17

Generic Graph Traversals Code

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

18

Depth First Search (1)

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

19

Depth First Search (2)

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

20

DFS Cost
DFS processes each edge once in a
directed graph.
In an undirected graph, DFS
processes each edge from both
directions.
Each vertex must be visited, but only
once.
So the total cost is: Cost: (|V| + |E|).
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

21

Illustration of DFS Process

C++3elatestP395.pdf

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

22

Breadth First Search (1)


Like DFS, but replace stack with a queue.
Visit vertexs neighbors before continuing
deeper in the tree.

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

23

Breadth First Search (2)

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

24

Breadth First Search (3)

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

25

Illustration of BFS Process

C++3elatestP397.pdf

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

26

Topological Sort (1)


Problem: Given a set of jobs, courses, etc.,
with prerequisite constraints, output the
jobs in an order that does not violate any
of the prerequisites.

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

27

Topological Sort (1)


We can model the problem using a
DAG.
The graph is directed because one task is
a prerequisite of another the vertices
have a directed relationship.
It is acyclic because a cycle would
indicate a conflicting series of
prerequisites that could not be completed
without violating at least one
prerequisite.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

28

Topological Sort (1)


The process of laying out the vertices of a
DAG in a linear order to meet the
prerequisite rules is called a topological sort.

An acceptable topological sort for this


example is: J1, J2, J3, J4, J5, J6, J7.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

29

Topological Sort (2)


A topological sort may be found by
performing a DFS on the graph.
When a vertex is visited, no action is taken
(i.e., function PreVisit does nothing).
When the recursion pops back to that
vertex, function PostVisit prints the
vertex.
This yields a topological sort in reverse
order.
Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

30

DFS Based Topological Sort

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

31

Topological Sorting
Example

Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

32

Topological Sorting
Example

9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

33

Topological Sorting
Example

8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

34

Topological Sorting
Example

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

35

Topological Sorting
Example

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

36

Topological Sorting
Example

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

37

Topological Sorting
Example

4
6

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

38

Topological Sorting
Example
3
4
6

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

39

Topological Sorting
Example
2

3
4
6

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

40

Topological Sorting
Example
2

3
4
6

7
8
9
Started on 09 April
2016

Course Teacher: Prof. Sohel


Rahman

41

Queue-Based Topsort
First visit all edges, counting the number of edges that
lead to each vertex
i.e., count the number of prerequisites for each vertex

All vertices with no prerequisites are placed on the


queue. And we begin processing the queue.
When Vertex V is taken off of the queue, it is printed,
and all neighbors of V have their counts decremented by
one.
All neighbours have V as a prerequisite

Place on the queue any neighbor whose count becomes


zero.
If the queue becomes empty without printing all of the
vertices, then the graph contains a cycle
i.e., there is no possible ordering for the tasks that
Course Teacher: Prof. Sohel
Started on 09 April 2016
42
does not violate some Rahman
prerequisite

Queue-Based Topsort

Started on 09 April 2016

Course Teacher: Prof. Sohel


Rahman

43

Вам также может понравиться