Unit ii divide and conquer -2

..

8/31/2020
1
UNIT-II
• Max-Min Problem
• Graphs problems: BFS
• DFS
• Articulation point
Material prepared by Dr. N. Subhash Chandra
From Web resources, Computer Algorithms , Horowitz, Sahni &
Rajasekaran
Max-Min Problem
TWO METHODS:
• Method 1 Straight forward: if we apply the general approach to
the array of size n, the number of comparisons required are 2n-2.
• Method-2: Divide and Conquer approach: we will divide the
problem into sub-problems and find the max and min of each group,
now max of each group will compare with the only max of another
group and min with min.
Problem: Analyze the algorithm to find the
maximum and minimum element from an
array.
1
2
8/31/2020
2
Straight forward Method: Max Min Problem
Divide Conquer method
Exercise problems: Construct Max-Min tree for
a) 25,13,8,50,98,-2,-17,45,55,88,97,5,-5
b) 78,-2,98,14,78,-10,67,45,39,100
3
4
8/31/2020
3
Divide and Conquer Method Algorithm
Analysis of Divide and Conquer Method
5
6
8/31/2020
4
Comparison of Straight forward method and
Divide and Conquer Method
Divide and Conquer the complexity 3n/2 – 2 is the best, average, worst case
number of comparison when n is a power of two.
Comparisons with Straight Forward Method:
Compared with the 2n – 2 comparisons for the Straight Forward method, this is a
saving of 25% in comparisons. It can be shown that no algorithm based on
comparisons uses less than 3n/2 – 2 comparisons.
Ex: n=8
Straight forward method: 2*8-2=14 comparisons
Divide and Conquer method: 3*8/2-2= 10 comparisons
Graph Traversal Algorithms
Traversing the graph means examining all the nodes and vertices of the
graph. There are two standard methods
• Breadth First Search
• Depth First Search
• Applications: Articulation point, Topological Sort, Convex hull
7
8
8/31/2020
5
Graph Applications
• Web pages with links
• Road maps (e.g., Google maps)
• Airline routes
• Facebook friends
• Family trees
• Paths through a maze
Paths
• path: A path from vertex a to b is a sequence of edges that
can be followed starting from a to reach b.
• can be represented as vertices visited, or edges taken
• example, one path from V to Z: {b, h} or {V, X, Z}
• What are two paths from U to Y?
• path length: Number of vertices
or edges contained in the path.
• neighbor or adjacent: Two vertices
connected directly by an edge.
• example: V and X
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
9
10
8/31/2020
6
Reachability, connectedness
• reachable: Vertex a is reachable from b
if a path exists from a to b.
• connected: A graph is connected if every
vertex is reachable from any other.
• Is the graph at top right connected?
• strongly connected: When every vertex
has an edge to every other vertex.
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
a
c
b
d
a
c
b
d
e
Loops and cycles
• cycle: A path that begins and ends at the same node.
• example: {b, g, f, c, a} or {V, X, Y, W, U, V}.
• example: {c, d, a} or {U, W, V, U}.
• acyclic graph: One that does
not contain any cycles.
• loop: An edge directly from
a node to itself.
• Many graphs don't allow loops.
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
11
12
8/31/2020
7
Weighted graphs
• weight: Cost associated with a given edge.
• Some graphs have weighted edges, and some are unweighted.
• Edges in an unweighted graph can be thought of as having equal
weight (e.g. all 0, or all 1, etc.)
• Most graphs do not allow negative weights.
• example: graph of airline flights, weighted by miles between
cities:
Colcatta
Chennai
Mumb
Bhuv
Lacknow
Patna
Hyd
Delhi
Directed graphs
• directed graph ("digraph"): One where edges are one-way
connections between vertices.
• If graph is directed, a vertex has a separate in/out degree.
• A digraph can be weighted or unweighted.
• Is the graph below connected? Why or why not?
a
d
b
e
gf
c
13
14
8/31/2020
8
Digraph example
• Vertices= UW CSE courses (incomplete list)
• Edge (a, b) = a is a prerequisite for b
142
143 154
140
311
312
331
351
333
341
344403
352
373
120
410
332
374
131
421431 440
415
413
417
414
444446
450
451
452
Linked Lists, Trees, Graphs
• A binary tree is a graph with some restrictions:
• The tree is an unweighted, directed, acyclic graph (DAG).
• Each node's in-degree is at most 1, and out-degree is at most 2.
• There is exactly one path from the root to every node.
• A linked list is also a graph:
• Unweighted DAG.
• In/out degree of at most 1 for all nodes.
F
B
A E
K
H
JG
A B DC
15
16
8/31/2020
9
Searching for paths
• Searching for a path from one vertex to another:
• Sometimes, we just want any path (or want to know there is a
path).
• Sometimes, we want to minimize path length (# of edges).
• Sometimes, we want to minimize path cost (sum of edge weights).
• What is the shortest path from MIA to SFO?
Which path has the minimum cost?
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
$500
Algorithms
Breadth First Search Depth First Search
• Visit start vertex and put into a FIFO
queue.
• Repeatedly remove a vertex from the
queue, visit its unvisited adjacent vertices,
put newly visited vertices into the queue.
17
18
8/31/2020
10
Example
• Breadth-first traversal using a queue.
Order of
Traversal Queue rearA B D E C G F H I
Queue front
A
B D E
C G
F H
I
BFS-tree:
Note: The BFS-tree for undirected graph is a free tree
Example
• Depth-first traversal using an explicit stack.
Order of
Traversal StackA B C F E G D H I
The Preorder Depth First Tree:
Note: The DFS-tree for undirected graph is a free tree
19
20
8/31/2020
11
Example to search G using DFS, BFS
Searching in BFS, DFS
21
22
8/31/2020
12
BFS, DFS Example
DFS, BFS Example
Starting the DFS at A, visit A -> B -> C,
backtrack to B, visit E -> F, backtrack to A,
visit D -> G -> H.
Starting the BFS at A, visit A -> B -> D -> C
-> E -> G -> H -> F.
23
24
8/31/2020
13
Exercise problems: Traverse by BFS,DFS
BFS,DFS Algorithms
25
26
8/31/2020
14
Difference b/w DFS, BFS
Complexities: When a vertex is
removed from the queue, we
examine its adjacent vertices.
 O(n) if adjacency matrix used
 O(vertex degree) if adjacency
lists used
Total time
 O(mn), where m is number
of vertices in the component
that is searched (adjacency
matrix) = O(V2)
 Adjacency list =O(V+E)
Application of DFS: Articulation point
27
28
8/31/2020
15
Articulation point
Bi-Connected Components
29
30
8/31/2020
16
Algorithm for Bi-Connected graph
Finding Articulation point
31
32
8/31/2020
17
Finding Articulation point using DFS
Finding Articulation point using DFS
33
34
8/31/2020
18
Example 1
Example 1
35
36
8/31/2020
19
Example 1
Vertex 1 2 3 4 5 6 7 8 9 10
DFN(u) 1 6 3 2 7 8 9 10 5 4
L(u) 1 1 1 1 6 8 6 6 5 4
Example 2
37
38
8/31/2020
20
Example 2
Example 2
Vertex 1 2 3 4 5 6 7 8
DFN(u) 1 2 3 6 4 5 7 8
L(u) 1 2 3 6 4 5 6 6
39
40
8/31/2020
21
Example 3
Example 3
41
42
8/31/2020
22
Example 3
Example 3 Vertex 1 2 3 4 5 6 7 8
DFN(u) 1 2 3 4 5 6 8 7
L(u) 1 1 1 1 1 4 3 4
43
44
8/31/2020
23
Exercise Problems on Articulation points using DFS
Problems
45
46
8/31/2020
24
Algorithm for Articulation point
Time complexity of
above method is
O(V*(V+E)) for a
graph represented
using adjacency
list.
Algorithm to determine Bi-Connected Components
47
48

Recomendados

Graphs in Data Structure por
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structurehafsa komal
2.2K visualizações22 slides
Graph Basic In Data structure por
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
2.1K visualizações12 slides
Graph in data structure por
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
198 visualizações28 slides
Basics of graph por
Basics of graphBasics of graph
Basics of graphKhaled Sany
588 visualizações13 slides
Graph in Data Structure por
Graph in Data StructureGraph in Data Structure
Graph in Data StructureProf Ansari
678 visualizações16 slides
Data structures and algorithms lab7 por
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
1.7K visualizações14 slides

Mais conteúdo relacionado

Mais procurados

Adjacency list por
Adjacency listAdjacency list
Adjacency listStefi Yu
4.5K visualizações15 slides
Data Structures - Lecture 10 [Graphs] por
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Muhammad Hammad Waseem
6.4K visualizações52 slides
Graph Data Structure por
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
610 visualizações41 slides
Graph representation por
Graph representationGraph representation
Graph representationTech_MX
37.2K visualizações34 slides
Graphs in data structure por
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
1.8K visualizações32 slides
Graphss por
GraphssGraphss
Graphssfika sweety
4.1K visualizações18 slides

Mais procurados(20)

Adjacency list por Stefi Yu
Adjacency listAdjacency list
Adjacency list
Stefi Yu4.5K visualizações
Data Structures - Lecture 10 [Graphs] por Muhammad Hammad Waseem
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem6.4K visualizações
Graph Data Structure por Keno benti
Graph Data StructureGraph Data Structure
Graph Data Structure
Keno benti610 visualizações
Graph representation por Tech_MX
Graph representationGraph representation
Graph representation
Tech_MX37.2K visualizações
Graphs in data structure por hamza javed
Graphs in data structureGraphs in data structure
Graphs in data structure
hamza javed1.8K visualizações
Graphss por fika sweety
GraphssGraphss
Graphss
fika sweety4.1K visualizações
Data structure computer graphs por Kumar
Data structure computer graphsData structure computer graphs
Data structure computer graphs
Kumar 4.4K visualizações
Graphs in data structures por Savit Chandra
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra1K visualizações
Lecture 14 data structures and algorithms por Aakash deep Singhal
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal1.8K visualizações
Tn 110 lecture 8 por ITNet
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
ITNet45 visualizações
Graphs por Zulkifal Yousaf
GraphsGraphs
Graphs
Zulkifal Yousaf1.7K visualizações
College Timetable Scheduling System por ShardulKulkarni24
College Timetable Scheduling SystemCollege Timetable Scheduling System
College Timetable Scheduling System
ShardulKulkarni2463 visualizações
Talk on Graph Theory - I por Anirudh Raja
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
Anirudh Raja4.2K visualizações
Graph in data structure por Abrish06
Graph in data structureGraph in data structure
Graph in data structure
Abrish0643K visualizações
Graph therory por mohanrathod18
Graph theroryGraph therory
Graph therory
mohanrathod1829 visualizações
Graphs bfs dfs por Jaya Gautam
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
Jaya Gautam17.1K visualizações
Graph theory por AparnaKumari31
Graph theoryGraph theory
Graph theory
AparnaKumari312.1K visualizações
Graph Data Structure por Afaq Mansoor Khan
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan300 visualizações
1.1.1B Measuring Segments por smiller5
1.1.1B Measuring Segments1.1.1B Measuring Segments
1.1.1B Measuring Segments
smiller51.2K visualizações

Similar a Unit ii divide and conquer -2

22-graphs1-dfs-bfs.ppt por
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.pptKarunaBiswas3
34 visualizações20 slides
LEC 12-DSALGO-GRAPHS(final12).pdf por
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfMuhammadUmerIhtisham
77 visualizações73 slides
U1 L5 DAA.pdf por
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdfLakshyaBaliyan2
2 visualizações47 slides
Unit ix graph por
Unit   ix    graph Unit   ix    graph
Unit ix graph Tribhuvan University
988 visualizações42 slides
Unit 9 graph por
Unit   9 graphUnit   9 graph
Unit 9 graphDabbal Singh Mahara
222 visualizações43 slides
Lecture 2.3.1 Graph.pptx por
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
33 visualizações23 slides

Similar a Unit ii divide and conquer -2(20)

22-graphs1-dfs-bfs.ppt por KarunaBiswas3
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
KarunaBiswas334 visualizações
LEC 12-DSALGO-GRAPHS(final12).pdf por MuhammadUmerIhtisham
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham77 visualizações
U1 L5 DAA.pdf por LakshyaBaliyan2
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
LakshyaBaliyan22 visualizações
Lecture 2.3.1 Graph.pptx por king779879
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
king77987933 visualizações
lec 09-graphs-bfs-dfs.ppt por TalhaFarooqui12
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
TalhaFarooqui124 visualizações
Minicourse on Network Science por Pavel Loskot
Minicourse on Network ScienceMinicourse on Network Science
Minicourse on Network Science
Pavel Loskot4.2K visualizações
Chapter 23 aoa por Hanif Durad
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
Hanif Durad209 visualizações
Dijkstra.ppt por Ruchika Sinha
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
Ruchika Sinha50 visualizações
Graph Theory por kailash shaw
Graph TheoryGraph Theory
Graph Theory
kailash shaw477 visualizações
Graph Theory por Rashmi Bhat
Graph TheoryGraph Theory
Graph Theory
Rashmi Bhat303 visualizações
Breadth first search por Sazzad Hossain
Breadth first searchBreadth first search
Breadth first search
Sazzad Hossain334 visualizações
Lecture 16 - Dijkstra's Algorithm.pdf por iftakhar8
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
iftakhar825 visualizações
Graph por Niliha
GraphGraph
Graph
Niliha56 visualizações
358 33 powerpoint-slides_13-graphs_chapter-13 por sumitbardhan
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan3.9K visualizações

Mais de subhashchandra197

Unit ii divide and conquer -4 por
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4subhashchandra197
25 visualizações36 slides
Unit ii divide and conquer -3 por
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3subhashchandra197
15 visualizações15 slides
Unit ii divide and conquer -1 por
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1subhashchandra197
19 visualizações15 slides
Bs,qs,divide and conquer 1 por
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1subhashchandra197
54 visualizações29 slides
Recursive algorithms por
Recursive algorithmsRecursive algorithms
Recursive algorithmssubhashchandra197
360 visualizações43 slides
Recurrence relation solutions por
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
981 visualizações56 slides

Mais de subhashchandra197(12)

Unit ii divide and conquer -4 por subhashchandra197
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
subhashchandra19725 visualizações
Unit ii divide and conquer -3 por subhashchandra197
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
subhashchandra19715 visualizações
Unit ii divide and conquer -1 por subhashchandra197
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
subhashchandra19719 visualizações
Bs,qs,divide and conquer 1 por subhashchandra197
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
subhashchandra19754 visualizações
Recursive algorithms por subhashchandra197
Recursive algorithmsRecursive algorithms
Recursive algorithms
subhashchandra197360 visualizações
Recurrence relation solutions por subhashchandra197
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
subhashchandra197981 visualizações
Asymptotic notations por subhashchandra197
Asymptotic notationsAsymptotic notations
Asymptotic notations
subhashchandra19765 visualizações
P,NP Hard, NP-Complete problems por subhashchandra197
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
subhashchandra19779 visualizações
Turing machine material por subhashchandra197
Turing machine materialTuring machine material
Turing machine material
subhashchandra19758 visualizações
Turing machine types por subhashchandra197
Turing machine typesTuring machine types
Turing machine types
subhashchandra19733 visualizações
Introduction to algorithms por subhashchandra197
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
subhashchandra197448 visualizações
Disjoint sets union, find por subhashchandra197
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
subhashchandra197525 visualizações

Último

CHEMICAL KINETICS.pdf por
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdfAguedaGutirrez
13 visualizações337 slides
DevOps-ITverse-2023-IIT-DU.pptx por
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptxAnowar Hossain
12 visualizações45 slides
SUMIT SQL PROJECT SUPERSTORE 1.pptx por
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptxSumit Jadhav
15 visualizações26 slides
Proposal Presentation.pptx por
Proposal Presentation.pptxProposal Presentation.pptx
Proposal Presentation.pptxkeytonallamon
42 visualizações36 slides
What is Unit Testing por
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
24 visualizações25 slides
fakenews_DBDA_Mar23.pptx por
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptxdeepmitra8
15 visualizações34 slides

Último(20)

CHEMICAL KINETICS.pdf por AguedaGutirrez
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdf
AguedaGutirrez13 visualizações
DevOps-ITverse-2023-IIT-DU.pptx por Anowar Hossain
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptx
Anowar Hossain12 visualizações
SUMIT SQL PROJECT SUPERSTORE 1.pptx por Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 15 visualizações
Proposal Presentation.pptx por keytonallamon
Proposal Presentation.pptxProposal Presentation.pptx
Proposal Presentation.pptx
keytonallamon42 visualizações
What is Unit Testing por Sadaaki Emura
What is Unit TestingWhat is Unit Testing
What is Unit Testing
Sadaaki Emura24 visualizações
fakenews_DBDA_Mar23.pptx por deepmitra8
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptx
deepmitra815 visualizações
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) por Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Tsuyoshi Horigome33 visualizações
Generative AI Models & Their Applications por SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 visualizações
Control Systems Feedback.pdf por LGGaming5
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdf
LGGaming56 visualizações
Searching in Data Structure por raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla637 visualizações
MSA Website Slideshow (16).pdf por msaucla
MSA Website Slideshow (16).pdfMSA Website Slideshow (16).pdf
MSA Website Slideshow (16).pdf
msaucla76 visualizações
_MAKRIADI-FOTEINI_diploma thesis.pptx por fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi8 visualizações
GDSC Mikroskil Members Onboarding 2023.pdf por gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil53 visualizações
Investor Presentation por eser sevinç
Investor PresentationInvestor Presentation
Investor Presentation
eser sevinç25 visualizações
Introduction to CAD-CAM.pptx por suyogpatil49
Introduction to CAD-CAM.pptxIntroduction to CAD-CAM.pptx
Introduction to CAD-CAM.pptx
suyogpatil495 visualizações
DESIGN OF SPRINGS-UNIT4.pptx por gopinathcreddy
DESIGN OF SPRINGS-UNIT4.pptxDESIGN OF SPRINGS-UNIT4.pptx
DESIGN OF SPRINGS-UNIT4.pptx
gopinathcreddy19 visualizações
Design of machine elements-UNIT 3.pptx por gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy32 visualizações
START Newsletter 3 por Start Project
START Newsletter 3START Newsletter 3
START Newsletter 3
Start Project5 visualizações

Unit ii divide and conquer -2

  • 1. 8/31/2020 1 UNIT-II • Max-Min Problem • Graphs problems: BFS • DFS • Articulation point Material prepared by Dr. N. Subhash Chandra From Web resources, Computer Algorithms , Horowitz, Sahni & Rajasekaran Max-Min Problem TWO METHODS: • Method 1 Straight forward: if we apply the general approach to the array of size n, the number of comparisons required are 2n-2. • Method-2: Divide and Conquer approach: we will divide the problem into sub-problems and find the max and min of each group, now max of each group will compare with the only max of another group and min with min. Problem: Analyze the algorithm to find the maximum and minimum element from an array. 1 2
  • 2. 8/31/2020 2 Straight forward Method: Max Min Problem Divide Conquer method Exercise problems: Construct Max-Min tree for a) 25,13,8,50,98,-2,-17,45,55,88,97,5,-5 b) 78,-2,98,14,78,-10,67,45,39,100 3 4
  • 3. 8/31/2020 3 Divide and Conquer Method Algorithm Analysis of Divide and Conquer Method 5 6
  • 4. 8/31/2020 4 Comparison of Straight forward method and Divide and Conquer Method Divide and Conquer the complexity 3n/2 – 2 is the best, average, worst case number of comparison when n is a power of two. Comparisons with Straight Forward Method: Compared with the 2n – 2 comparisons for the Straight Forward method, this is a saving of 25% in comparisons. It can be shown that no algorithm based on comparisons uses less than 3n/2 – 2 comparisons. Ex: n=8 Straight forward method: 2*8-2=14 comparisons Divide and Conquer method: 3*8/2-2= 10 comparisons Graph Traversal Algorithms Traversing the graph means examining all the nodes and vertices of the graph. There are two standard methods • Breadth First Search • Depth First Search • Applications: Articulation point, Topological Sort, Convex hull 7 8
  • 5. 8/31/2020 5 Graph Applications • Web pages with links • Road maps (e.g., Google maps) • Airline routes • Facebook friends • Family trees • Paths through a maze Paths • path: A path from vertex a to b is a sequence of edges that can be followed starting from a to reach b. • can be represented as vertices visited, or edges taken • example, one path from V to Z: {b, h} or {V, X, Z} • What are two paths from U to Y? • path length: Number of vertices or edges contained in the path. • neighbor or adjacent: Two vertices connected directly by an edge. • example: V and X XU V W Z Y a c b e d f g h 9 10
  • 6. 8/31/2020 6 Reachability, connectedness • reachable: Vertex a is reachable from b if a path exists from a to b. • connected: A graph is connected if every vertex is reachable from any other. • Is the graph at top right connected? • strongly connected: When every vertex has an edge to every other vertex. XU V W Z Y a c b e d f g h a c b d a c b d e Loops and cycles • cycle: A path that begins and ends at the same node. • example: {b, g, f, c, a} or {V, X, Y, W, U, V}. • example: {c, d, a} or {U, W, V, U}. • acyclic graph: One that does not contain any cycles. • loop: An edge directly from a node to itself. • Many graphs don't allow loops. XU V W Z Y a c b e d f g h 11 12
  • 7. 8/31/2020 7 Weighted graphs • weight: Cost associated with a given edge. • Some graphs have weighted edges, and some are unweighted. • Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1, etc.) • Most graphs do not allow negative weights. • example: graph of airline flights, weighted by miles between cities: Colcatta Chennai Mumb Bhuv Lacknow Patna Hyd Delhi Directed graphs • directed graph ("digraph"): One where edges are one-way connections between vertices. • If graph is directed, a vertex has a separate in/out degree. • A digraph can be weighted or unweighted. • Is the graph below connected? Why or why not? a d b e gf c 13 14
  • 8. 8/31/2020 8 Digraph example • Vertices= UW CSE courses (incomplete list) • Edge (a, b) = a is a prerequisite for b 142 143 154 140 311 312 331 351 333 341 344403 352 373 120 410 332 374 131 421431 440 415 413 417 414 444446 450 451 452 Linked Lists, Trees, Graphs • A binary tree is a graph with some restrictions: • The tree is an unweighted, directed, acyclic graph (DAG). • Each node's in-degree is at most 1, and out-degree is at most 2. • There is exactly one path from the root to every node. • A linked list is also a graph: • Unweighted DAG. • In/out degree of at most 1 for all nodes. F B A E K H JG A B DC 15 16
  • 9. 8/31/2020 9 Searching for paths • Searching for a path from one vertex to another: • Sometimes, we just want any path (or want to know there is a path). • Sometimes, we want to minimize path length (# of edges). • Sometimes, we want to minimize path cost (sum of edge weights). • What is the shortest path from MIA to SFO? Which path has the minimum cost? ORD PVD MIA DFW SFO LAX LGA HNL $500 Algorithms Breadth First Search Depth First Search • Visit start vertex and put into a FIFO queue. • Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue. 17 18
  • 10. 8/31/2020 10 Example • Breadth-first traversal using a queue. Order of Traversal Queue rearA B D E C G F H I Queue front A B D E C G F H I BFS-tree: Note: The BFS-tree for undirected graph is a free tree Example • Depth-first traversal using an explicit stack. Order of Traversal StackA B C F E G D H I The Preorder Depth First Tree: Note: The DFS-tree for undirected graph is a free tree 19 20
  • 11. 8/31/2020 11 Example to search G using DFS, BFS Searching in BFS, DFS 21 22
  • 12. 8/31/2020 12 BFS, DFS Example DFS, BFS Example Starting the DFS at A, visit A -> B -> C, backtrack to B, visit E -> F, backtrack to A, visit D -> G -> H. Starting the BFS at A, visit A -> B -> D -> C -> E -> G -> H -> F. 23 24
  • 13. 8/31/2020 13 Exercise problems: Traverse by BFS,DFS BFS,DFS Algorithms 25 26
  • 14. 8/31/2020 14 Difference b/w DFS, BFS Complexities: When a vertex is removed from the queue, we examine its adjacent vertices.  O(n) if adjacency matrix used  O(vertex degree) if adjacency lists used Total time  O(mn), where m is number of vertices in the component that is searched (adjacency matrix) = O(V2)  Adjacency list =O(V+E) Application of DFS: Articulation point 27 28
  • 16. 8/31/2020 16 Algorithm for Bi-Connected graph Finding Articulation point 31 32
  • 17. 8/31/2020 17 Finding Articulation point using DFS Finding Articulation point using DFS 33 34
  • 19. 8/31/2020 19 Example 1 Vertex 1 2 3 4 5 6 7 8 9 10 DFN(u) 1 6 3 2 7 8 9 10 5 4 L(u) 1 1 1 1 6 8 6 6 5 4 Example 2 37 38
  • 20. 8/31/2020 20 Example 2 Example 2 Vertex 1 2 3 4 5 6 7 8 DFN(u) 1 2 3 6 4 5 7 8 L(u) 1 2 3 6 4 5 6 6 39 40
  • 22. 8/31/2020 22 Example 3 Example 3 Vertex 1 2 3 4 5 6 7 8 DFN(u) 1 2 3 4 5 6 8 7 L(u) 1 1 1 1 1 4 3 4 43 44
  • 23. 8/31/2020 23 Exercise Problems on Articulation points using DFS Problems 45 46
  • 24. 8/31/2020 24 Algorithm for Articulation point Time complexity of above method is O(V*(V+E)) for a graph represented using adjacency list. Algorithm to determine Bi-Connected Components 47 48