cse 203-lec7
TRANSCRIPT
-
8/16/2019 CSE 203-Lec7
1/43
CSE 203: Data Structures
Lecture 5: Graphs
Started on 09 April 2016 1Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
2/43
Graphs
A graphG = (V,E) consists of a set ofverticesV, and a set of edgesE, such thateach edge inE is a connection between apair of vertices inV.
The number of vertices is written |V|, and thenumber edges is written |E|.
Started on 09 April 2016 2Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
3/43
Graphs (2)
Started on 09 April 2016 3Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
4/43
Paths and Cycles
Path: A sequence of verticesv1,v2, …,vn of length
n-1 with an edge fromvi tov
i+1 for 1
-
8/16/2019 CSE 203-Lec7
5/43
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
5
• There is a si$ple path !ro$ &erte' 0 to &erte' 3 containin(
&ertices 0) 1) and 3"• &ertices 0) %) 2) 3) 1) %) 2) 3) 1 also !or$ a path) *ut not a
si$ple path *ecause so$e +ertices appear t,ice"
• &ertices 0) 1) %) and 0 !or$ a si$ple c-cle"
-
8/16/2019 CSE 203-Lec7
6/43
Connected Components•
A su*(raph S is !or$ed !ro$ a (raph G = (V,E) *- selectin( a su*set V_s o! & a su*set E.so! E such that !or e+er- ed(e e in E.s) *oth o!e/s +ertices are in &.s"
• An undirected graph is connected if there is at leastone path from any vertex to any other.
• The maximum connected subgraphs of anundirected graph are called connected components.
Started on 09 Ap
ril 2016 6Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
7/43
Ac-clic (raphs
• A (raph ,ithout c-cles is calledac-clic"
• Thus) a directed (raph ,ithout c-clesis called a directed ac-clic (raph orDAG"
• A !ree tree is a connected) undirected(raph ,ith no si$ple c-cles" – An eui+alent denition is that a !ree
tree is connected and has |V|-1 ed(es"
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
8/43
Directed Representation
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
9/43
Undirected Representation
Started on 09 April 2016 9Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
10/43
Co$parison
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
10
Adjacency List Adjacency MatrixStores in!or$ation onl- !orthose ed(es that actuall-appear in the (raph
#euires space !or eachpotential ed(e) ,hether ite'ists or not
4eeds pointers" #euires no o+erhead !orpointers) ,hich can *e asu*stantial cost) especiall- i!the onl- in!or$ation stored!or an ed(e is one *it to
indicate its e'istence"Sparse (raphs are liel- toha+e their adacenc- listrepresentation *e $orespace e7cient"
8o,e+er) As the (raph*eco$es denser) theadacenc- $atri' *eco$esrelati+el- $ore space
e7cient"
-
8/16/2019 CSE 203-Lec7
11/43
Space and Time
Adjacency Matrix: O(|V̂2|)
Adjacency List: O(|V| + |E|)
Started on 09 April 2016 11Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
12/43
Graph ADT
• e assu$e that – +ertices are dened *- an inte(er inde'
+alue"• there is a &erte' 0) &erte' 1) and so on
– There is a use!ul arra- called ar
C;;3elatest3"pd!
Started on 09 April 2016 12Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
13/43
atri'
-
8/16/2019 CSE 203-Lec7
14/43
List
-
8/16/2019 CSE 203-Lec7
15/43
Graph Traversals
Some applications require visiting everyvertex in the graph exactly once.
The application may require that vertices bevisited in some special order based ongraph topology.
Examples: –Artificial Intelligence Search –Shortest paths problems
Started on 09 April 2016 15Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
16/43
Graph Traversals Difficulties
• >irst) it $a- not *e possi*le to reachall +ertices !ro$ the start +erte'" – This occurs ,hen the (raph is not
connected
• Second) the (raph $a- containc-cles) and ,e $ust $ae sure that
c-cles do not cause the al(orith$ to(o into an innite loop"
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
16
-
8/16/2019 CSE 203-Lec7
17/43
Graph Traversals Difficulties: Solutions
• Graph tra+ersal al(orith$s can sol+e *oth o! thesepro*le$s *- $aintainin( a $ar *it !or each +erte'on the (raph" – At the *e(innin( o! the al(orith$) the $ar *it !or all
+ertices is cleared"
– The $ar *it !or a +erte' is set ,hen the +erte' is rst+isited durin( the tra+ersal"
–
1
-
8/16/2019 CSE 203-Lec7
18/43
Generic Graph Traversals Code
Started on 09 April 2016 1Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
19/43
Depth First Search (1)
Started on 09 April 2016 19Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
20/43
Depth First Search (2)
Started on 09 April 2016 20Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
21/43
D>S Cost
• D>S processes each ed(e once in adirected (raph"
• Sprocesses each ed(e !ro$ *othdirections"
• Each +erte' $ust *e +isited) *ut onl-
once"
• So the total cost is: Cost:Θ(|V| + |E|).
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
21
-
8/16/2019 CSE 203-Lec7
22/43
S rocess
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
22
C;;3elatest395"pd!
-
8/16/2019 CSE 203-Lec7
23/43
Breadth First Search (1)
Like DFS, but replace stack with a queue. –Visit vertex’s neighbors before continuingdeeper in the tree.
Started on 09 April 2016 23Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
24/43
Breadth First Search (2)
Started on 09 April 2016 2%Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
25/43
Breadth First Search (3)
Started on 09 April 2016 25Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
26/43
S rocess
Started on 09 April 2016 Course Teacher: ro!" Sohel#ah$an
26
C;;3elatest39"pd!
-
8/16/2019 CSE 203-Lec7
27/43
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 2Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
28/43
Topological Sort (1)
• e can $odel the pro*le$ usin( aDAG" – The (raph is directed *ecause one tas is
a prereuisite o! another the +erticesha+e a directed relationship"
–
-
8/16/2019 CSE 203-Lec7
29/43
Topological Sort (1)
• The process o! la-in( out the +ertices o! aDAG in a linear order to $eet theprereuisite rules is called a topolo(ical sort"
An accepta*le topolo(ical sort !or this
e'a$ple is: 1) 2) 3) %) 5) 6) "Started on 09 April 2016 29Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
30/43
Topological Sort (2)
• A topolo(ical sort $a- *e !ound *-per!or$in( a D>S on the (raph"
• hen a +erte' is +isited) no action is taen
i"e") !unction PreVisit does nothing).• When the recursion pops *ac to that
+erte') !unction PostVisit prints thevertex.
• This -ields a topolo(ical sort in re+erseorder"
Started on 09 April 2016 30Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
31/43
DFS Based Topological Sort
Started on 09 April 2016 31Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
32/43
Course Teacher: ro!" Sohel#ah$an 32
Topolo(ical Sortin(
E'a$ple
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
33/43
Course Teacher: ro!" Sohel#ah$an 33
Topolo(ical Sortin(
E'a$ple
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
34/43
Course Teacher: ro!" Sohel#ah$an 3%
Topolo(ical Sortin(
E'a$ple
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
35/43
Course Teacher: ro!" Sohel#ah$an 35
Topolo(ical Sortin(
E'a$ple
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
36/43
Course Teacher: ro!" Sohel#ah$an 36
Topolo(ical Sortin(
E'a$ple
6
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
37/43
Course Teacher: ro!" Sohel#ah$an 3
Topolo(ical Sortin(
E'a$ple
56
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
38/43
Course Teacher: ro!" Sohel#ah$an 3
Topolo(ical Sortin(
E'a$ple
%
56
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
39/43
Course Teacher: ro!" Sohel#ah$an 39
Topolo(ical Sortin(
E'a$ple
%
56
3
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
40/43
Course Teacher: ro!" Sohel#ah$an %0
Topolo(ical Sortin(
E'a$ple 2
%
56
3
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
41/43
Course Teacher: ro!" Sohel#ah$an %1
Topolo(ical Sortin(
E'a$ple 2
%
56
1
3
9
Started on 09 April2016
-
8/16/2019 CSE 203-Lec7
42/43
Queue-Based Topsort• >irst +isit all ed(es) countin( the nu$*er o! ed(es that
lead to each +erte' – i"e") count the nu$*er o! prereuisites !or each +erte'
• All +ertices ,ith no prereuisites are placed on theueue" And ,e *e(in processin( the ueue"
• hen &erte' & is taen o o! the ueue) it is printed)and all nei(h*ors o! & ha+e their counts decre$ented *-one" – All nei(h*ours ha+e & as a prereuisite
•
lace on the ueue an- nei(h*or ,hose count *eco$esFero"
•
– i"e") there is no possi*le orderin( !or the tass that
does not +iolate so$e prereuisiteStarted on 09 April 2016 %2Course Teacher: ro!" Sohel#ah$an
-
8/16/2019 CSE 203-Lec7
43/43
Queue-Based Topsort