depth first search alg

Upload: mdhuq1

Post on 05-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Depth First Search Alg

    1/44

    Depth First Search AlgorithmDepth First Search Algorithm

    Algorithm DFS(G)Algorithm DFS(G)

    // Implements a depth first search traversal of a given graph// Implements a depth first search traversal of a given graph

    // Input : Graph = (V,E)// Input : Graph = (V,E)// Output : Graph g with its vertices marked with consecutive// Output : Graph g with its vertices marked with consecutive

    // integers in the order they have been first// integers in the order they have been first

    // encountered by the DFS traversal mark each vertex// encountered by the DFS traversal mark each vertex

    // in V with 0 as a mark of being unvisited// in V with 0 as a mark of being unvisited

    count 0count 0

    for each vertex v in V dofor each vertex v in V do

    if v is marked with 0if v is marked with 0dfs(v)dfs(v)

  • 7/31/2019 Depth First Search Alg

    2/44

    Depth First Search AlgorithmDepth First Search Algorithm

    dfs(v)dfs(v)

    // visits recursively all the unvisited vertices connected to vertex// visits recursively all the unvisited vertices connected to vertex

    // v and assigns them the numbers in the order they are// v and assigns them the numbers in the order they are// encountered via global variable count// encountered via global variable count

    count count + 1; mark v with countcount count + 1; mark v with count

    for each vertex w in V adjacent to v dofor each vertex w in V adjacent to v do

    if w is marked with 0if w is marked with 0

    dfs(w)dfs(w)

  • 7/31/2019 Depth First Search Alg

    3/44

    Depth First Search AlgorithmDepth First Search Algorithm With the adjacency matrix representation of the graph , theWith the adjacency matrix representation of the graph , the

    traversals time efficiency is intraversals time efficiency is in (|V|(|V|22) and for the adjacency) and for the adjacencylinked list representation , it is inlinked list representation , it is in (|V| + |E|) , where |V|(|V| + |E|) , where |V|

    and |E| are the number of the graphs vertices and edgesand |E| are the number of the graphs vertices and edgesrespectively.respectively.

    Important elementary application of DFS include checkingImportant elementary application of DFS include checkingconnectivity and checking acyclycity of a Graph.connectivity and checking acyclycity of a Graph.

    Graphs connectivity can be done as follows.Graphs connectivity can be done as follows. Start a DFS traversal at an arbitrary vertex and check , afterStart a DFS traversal at an arbitrary vertex and check , after

    the algorithms halts, whether all the graphs vertices willthe algorithms halts, whether all the graphs vertices willhave been visited. If they have , the graph is connected ,have been visited. If they have , the graph is connected ,otherwise it is not connected.otherwise it is not connected.

    If there is a back edge from the vertex to its ancestor thenIf there is a back edge from the vertex to its ancestor thenthe graph has a cycle.the graph has a cycle.

    A vertex of a connected graph is said to be its articulationA vertex of a connected graph is said to be its articulationpoint it its removal with all edges incident to it brakes thepoint it its removal with all edges incident to it brakes the

    graph into disjoint pieces.graph into disjoint pieces.

  • 7/31/2019 Depth First Search Alg

    4/44

    Breadth First Search AlgorithmBreadth First Search Algorithm It proceeds in a concentric manner by visiting first all theIt proceeds in a concentric manner by visiting first all the

    vertices that are adjacent to a starting vertex, then allvertices that are adjacent to a starting vertex, then allunvisited vertices that are adjacent to a starting vertex, thenunvisited vertices that are adjacent to a starting vertex, then

    all unvisited vertices two edges apart from it , and so on ,all unvisited vertices two edges apart from it , and so on ,until all the vertices in the same connected component asuntil all the vertices in the same connected component asthe starting vertex are visited.the starting vertex are visited.

    If there still remain unvisited vertices , the algorithm has toIf there still remain unvisited vertices , the algorithm has to

    be restarted at an arbitrary vertex of another connectedbe restarted at an arbitrary vertex of another connectedcomponent of the graph.component of the graph.

    It is convenient to use a Queue to trace the operation ofIt is convenient to use a Queue to trace the operation ofBreadth First Search.Breadth First Search.

    The Queue is initialized with the traversals starting vertex,The Queue is initialized with the traversals starting vertex,

    which is marked as visited.which is marked as visited. On each iteration, the algorithm identifies all unvisitedOn each iteration, the algorithm identifies all unvisited

    vertices that are adjacent to the front vertex, marks them asvertices that are adjacent to the front vertex, marks them asvisited , and adds them to the queue, after that , the frontvisited , and adds them to the queue, after that , the frontvertex is removed from the queue.vertex is removed from the queue.

  • 7/31/2019 Depth First Search Alg

    5/44

  • 7/31/2019 Depth First Search Alg

    6/44

    Breadth First Search AlgorithmBreadth First Search Algorithmcount 0count 0

    for each vertex v in V dofor each vertex v in V do

    if v is marked with 0if v is marked with 0

    bfs(v)bfs(v)

    bfs(v)bfs(v)

    // visits recursively all the unvisited vertices connected to vertex// visits recursively all the unvisited vertices connected to vertex

    // v and assigns them the numbers in the order they are// v and assigns them the numbers in the order they are

    // encountered via global variable count// encountered via global variable count

    count count + 1; mark v with count and initialize a queuecount count + 1; mark v with count and initialize a queuewith vwith v

    while the queue is not empty dowhile the queue is not empty dofor each vertex w in V adjacent to the fronts vertex v dofor each vertex w in V adjacent to the fronts vertex v do

    if w is marked with 0if w is marked with 0

    count count + 1; mark w with count add w to the queuecount count + 1; mark w with count add w to the queue

    remove vertex v from the front of the queue.remove vertex v from the front of the queue.

  • 7/31/2019 Depth First Search Alg

    7/44

    Breadth First Search AlgorithmBreadth First Search Algorithm

    ..With the adjacency matrix representation of the graph , theWith the adjacency matrix representation of the graph , the

    traversals time efficiency is intraversals time efficiency is in (|V|(|V|22) and for the adjacency) and for the adjacency

    linked list representation , it is inlinked list representation , it is in (|V| + |E|) , where |V|(|V| + |E|) , where |V|

    and |E| are the number of the graphs vertices and edgesand |E| are the number of the graphs vertices and edges

    respectively.respectively.

    Important elementary application of DFS include checkingImportant elementary application of DFS include checking

    connectivity and checking acyclycity of a Graph.connectivity and checking acyclycity of a Graph.

    a cb d

    e f g i

    Draw the BFS and DFS for the following graphs

  • 7/31/2019 Depth First Search Alg

    8/44

    Directed Graph Basic ConceptsDirected Graph Basic Concepts A directed Graph or Digraph is a graph with directionsA directed Graph or Digraph is a graph with directions

    specified for all its edges.specified for all its edges.

    A Digraph can be represented by Adjacency Matrix andA Digraph can be represented by Adjacency Matrix and

    Adjacency Linked list.Adjacency Linked list.

    There two basic differences between Directed graph andThere two basic differences between Directed graph and

    Undirected Graph .Undirected Graph .

    1.1.

    The Adjacency matrix of a Directed graph does not have to beThe Adjacency matrix of a Directed graph does not have to be

    SymmetricSymmetric

    2.2. An edge in a Digraph has just one (not two) correspondingAn edge in a Digraph has just one (not two) corresponding

    nodes in the digraphs adjacency linked lists.nodes in the digraphs adjacency linked lists.

    Depth First Search forest exhibits all four types of edgesDepth First Search forest exhibits all four types of edgespossible in a DFS forest of a directed graph.possible in a DFS forest of a directed graph.

  • 7/31/2019 Depth First Search Alg

    9/44

    a b

    c

    d

    e

    Digraph

    a

    b

    c

    d

    e

    DFS forest of the digraph for the DFStraversal started at a

    1.1. Tree edges( ab, bc, de)Tree edges( ab, bc, de)

    2.2. Back edges (ba) from the vertices to their ancestorsBack edges (ba) from the vertices to their ancestors

    3.3. Forward edges (ac) from vertices to their descendants in theForward edges (ac) from vertices to their descendants in the

    tree other than their childrentree other than their children

    4.4. Cross edge (dc) .Cross edge (dc) .

  • 7/31/2019 Depth First Search Alg

    10/44

    A directed cycle in a digraph is a sequence of its vertices thatA directed cycle in a digraph is a sequence of its vertices that

    starts and ends at the same vertex in which every vertex isstarts and ends at the same vertex in which every vertex is

    connected to its immediate predecessor by an edge directedconnected to its immediate predecessor by an edge directed

    from the predecessor to the successor.from the predecessor to the successor. If a DFS forest of a directed graph has no back edges , theIf a DFS forest of a directed graph has no back edges , the

    digraph is a dag ( directed acyclic graph).digraph is a dag ( directed acyclic graph).

  • 7/31/2019 Depth First Search Alg

    11/44

    Topological SortingTopological Sorting Consider a set of five required courses {C1,C2,C3,C4,C5} a part-Consider a set of five required courses {C1,C2,C3,C4,C5} a part-

    time student has to take in some degree program.time student has to take in some degree program.

    The courses can be taken in any order as long as the followingThe courses can be taken in any order as long as the followingcourse prerequisites are met :course prerequisites are met :

    C1 and C2 has no prerequisites . C3 requires C1 and C2 , C4C1 and C2 has no prerequisites . C3 requires C1 and C2 , C4

    requires C3, and C5 requires C3 and C4.requires C3, and C5 requires C3 and C4.

    The Student can take only one course per term. In which orderThe Student can take only one course per term. In which ordershould the student take the courses?should the student take the courses?

    The situation can be modeled by a graph in which verticesThe situation can be modeled by a graph in which vertices

    represent courses and directed edges indicate prerequisiterepresent courses and directed edges indicate prerequisite

    requirements.requirements.

    In terms of this digraph, the question is whether we can list itsIn terms of this digraph, the question is whether we can list its

    vertices in such an order that, for every edge in the graph, thevertices in such an order that, for every edge in the graph, the

    vertex where the edge starts is listed before the vertex wherevertex where the edge starts is listed before the vertex where

    the edge ends. Can you find such ordering of this digraphsthe edge ends. Can you find such ordering of this digraphs

    vertices? This problem is called Topological Sorting.vertices? This problem is called Topological Sorting.

  • 7/31/2019 Depth First Search Alg

    12/44

    It can be posed for an arbitrary digraph, but it is easy to see thatIt can be posed for an arbitrary digraph, but it is easy to see that

    the problem cannot have a solution if a digraph has a directedthe problem cannot have a solution if a digraph has a directed

    cycle.cycle.

    Thus, for topological sorting to be possible, a digraph mustbeThus, for topological sorting to be possible, a digraph mustbe

    dag.dag.

    If a digraph has no cycles , the topological sorting problem for itIf a digraph has no cycles , the topological sorting problem for it

    has a solution.has a solution.

    There are two efficient algorithms that both verify whether aThere are two efficient algorithms that both verify whether a

    digraph is a dag and if it is , produce an ordering of vertices thatdigraph is a dag and if it is , produce an ordering of vertices that

    solves the topological sorting problem.solves the topological sorting problem.

    First algorithm is simple application of the DFS. Perform a DFSFirst algorithm is simple application of the DFS. Perform a DFS

    traversal and note the order in which vertices become deadtraversal and note the order in which vertices become dead

    ends. Reversing this order yields a solution to the topologicalends. Reversing this order yields a solution to the topologicalsorting problem provided. Of course , no back edge has beensorting problem provided. Of course , no back edge has been

    encountered, the digraph is not a dag and topological sorting ofencountered, the digraph is not a dag and topological sorting of

    its vertices is impossible.its vertices is impossible.

  • 7/31/2019 Depth First Search Alg

    13/44

    The second algorithm is based on a direct implementation of theThe second algorithm is based on a direct implementation of the

    Decrease by One technique, repeatedly , identify in a remainingDecrease by One technique, repeatedly , identify in a remaining

    digraph a source, which is a vertex with no incoming edges, anddigraph a source, which is a vertex with no incoming edges, and

    delete it along with all edges outgoing from it.delete it along with all edges outgoing from it.

    If there are several sources , break the tie arbitrarily .If there isIf there are several sources , break the tie arbitrarily .If there is

    none , stop because the problem can not be solved.none , stop because the problem can not be solved.

    The order in which the vertices are deleted yields a solution toThe order in which the vertices are deleted yields a solution to

    the topological sorting problem.the topological sorting problem.

    C1

    C3

    C4

    C2 C5

    Digraph of the topological

    sorting Problem

    C2

    C3

    C4

    C5

    Delete C1

  • 7/31/2019 Depth First Search Alg

    14/44

    C3

    C5

    C4

    Delete C2 Delete C3

    C4

    C5

    Delete C4

    C5

    Delete C5

    The Solution obtained is C1,C2,C3,C4,C5

  • 7/31/2019 Depth First Search Alg

    15/44

    Imagine a large project e.g., in construction or researchImagine a large project e.g., in construction or research

    that involves thousands of interrelated tasks with that involves thousands of interrelated tasks with

    known prerequisites.known prerequisites.

    The first thing is to make sure that the set of givenThe first thing is to make sure that the set of given

    prerequisites is not contradictory.prerequisites is not contradictory.

    The convenient way of doing this is to solve theThe convenient way of doing this is to solve the

    topological sorting problem for the projects digraph.topological sorting problem for the projects digraph.

    Only then we can start scheduling the tasks to minimizeOnly then we can start scheduling the tasks to minimize

    the total completion of the project.the total completion of the project.

  • 7/31/2019 Depth First Search Alg

    16/44

    Transform-and-ConquerTransform-and-Conquer It deals with a group of design methods that are based onIt deals with a group of design methods that are based on

    the idea of transformation. This technique is known asthe idea of transformation. This technique is known asTransform and conquer because these methods work asTransform and conquer because these methods work astwo stage procedures.two stage procedures.

    1.1. In the Transformation stage , the problems instance isIn the Transformation stage , the problems instance ismodified to be , for one reason to another ,more amenablemodified to be , for one reason to another ,more amenableto solution.to solution.

    2.2. In the conquering stage , it is solved.In the conquering stage , it is solved. There three major variations in Transform a given instanceThere three major variations in Transform a given instance

    1.1. Transformation to a simpler or more convenient instance ofTransformation to a simpler or more convenient instance ofthe same problem is known asthe same problem is known as Instance SimplificationInstance Simplification..

    2.2. Transformation to a different representation of the sameTransformation to a different representation of the sameinstance is known asinstance is known as Representation ChangeRepresentation Change..

    3.3. Transformation to an instance of a different problem forTransformation to an instance of a different problem forwhich an algorithm is already available is known aswhich an algorithm is already available is known as ProblemProblemreductionreduction..

    f d

  • 7/31/2019 Depth First Search Alg

    17/44

    Transform-and-Conquer StrategyTransform-and-Conquer Strategy

    Problems

    Instance

    Simplerinstance

    Another

    representation

    AnotherProblemsinstance

    Solution

  • 7/31/2019 Depth First Search Alg

    18/44

    Instance simplification - PresortingInstance simplification - Presorting

    PresortingPresorting

    Many problems involving lists are easier when list is sorted.Many problems involving lists are easier when list is sorted.

    checking if all elements are distinct (element uniqueness)checking if all elements are distinct (element uniqueness)

    Computing a modeComputing a mode Searching problemSearching problem

  • 7/31/2019 Depth First Search Alg

    19/44

    Element Uniqueness with presortingElement Uniqueness with presorting

    Brute force algorithmBrute force algorithm

    Compare all pairs of elementsCompare all pairs of elements

    Efficiency: O(Efficiency: O(nn22))

    Presorting-based algorithmPresorting-based algorithmStage 1: sort by efficient sorting algorithm (e.g.Stage 1: sort by efficient sorting algorithm (e.g.

    mergesort)mergesort)

    Stage 2: scan array to check pairs ofStage 2: scan array to check pairs ofadjacentadjacent elementselements

  • 7/31/2019 Depth First Search Alg

    20/44

    Presorting-based algorithmPresorting-based algorithm

    ALGORITHMALGORITHM

    Efficiency?Efficiency?

  • 7/31/2019 Depth First Search Alg

    21/44

    Computing a ModeComputing a Mode

    A mode is a value that occurs most often in a given list ofA mode is a value that occurs most often in a given list of

    numbers.numbers.

    E.g., for 4, 1, 6, 2, 1, 4, 1, 5, the mode isE.g., for 4, 1, 6, 2, 1, 4, 1, 5, the mode is

    Brute-force algorithm:Brute-force algorithm:

    scan the list and compute the frequencies of all its distinctscan the list and compute the frequencies of all its distinct

    valuesvalues

    Find the value with the largest frequencyFind the value with the largest frequency

    Worst-case performance of Brute-force approach:?Worst-case performance of Brute-force approach:?

    Presorting:Presorting:

    Sort the inputSort the input Find the longest run of adjacent equal values in the sortedFind the longest run of adjacent equal values in the sorted

    arrayarray

    Worst-case running time of PresortMode:?Worst-case running time of PresortMode:?

  • 7/31/2019 Depth First Search Alg

    22/44

    ALGORITHMALGORITHM

  • 7/31/2019 Depth First Search Alg

    23/44

  • 7/31/2019 Depth First Search Alg

    24/44

    Heaps and HeapsortHeaps and Heapsort

    DefinitionDefinition AAheapheapis a binary tree with keys at its nodesis a binary tree with keys at its nodes

    (one key per node) such that:(one key per node) such that: It is essentially complete, i.e., all its levels are full exceptIt is essentially complete, i.e., all its levels are full except

    possibly the last level, where only some rightmost keyspossibly the last level, where only some rightmost keys

    may be missingmay be missing

    The key at each node isThe key at each node is keys at its childrenkeys at its children

  • 7/31/2019 Depth First Search Alg

    25/44

    Illustration of the heaps definitionIllustration of the heaps definition

    10

    5

    4 2

    7

    1

    10

    5

    2

    7

    1

    10

    5

    6 2

    7

    1

    a heapa heap not a heapnot a heap not a heapnot a heap

    Note: Heaps elements are ordered top down (along any pathNote: Heaps elements are ordered top down (along any path

    down from its root), but they are not ordered left to rightdown from its root), but they are not ordered left to right

  • 7/31/2019 Depth First Search Alg

    26/44

    Some Important Properties of a HeapSome Important Properties of a Heap

    GivenGiven n,n, there exists a unique binary tree withthere exists a unique binary tree with nnnodesnodes

    thatthat

    is essentially complete, withis essentially complete, with hh== loglog22 nn

    The root contains the largest keyThe root contains the largest key

    The subtree rooted at any node of a heap is also a heapThe subtree rooted at any node of a heap is also a heap

    A heap can be represented as an arrayA heap can be represented as an array

  • 7/31/2019 Depth First Search Alg

    27/44

    Heaps Array RepresentationHeaps Array Representation

    Store heaps elements in an array (whose elements indexed, forStore heaps elements in an array (whose elements indexed, forconvenience, 1 toconvenience, 1 to nn) in top-down left-to-right order) in top-down left-to-right order

    Example:Example:

    Left child of nodeLeft child of nodejjis at 2is at 2jj Right child of nodeRight child of nodejjis at 2is at 2jj+1+1 Parent of nodeParent of nodejjis atis at jj/2/2 Parental nodes are represented in the firstParental nodes are represented in the first nn/2/2 locationslocations

    9

    1

    5 3

    4 2

    1 2 3 4 5 6

    H C t ti (b tt )Heap Construction (bottom up)

  • 7/31/2019 Depth First Search Alg

    28/44

    Step 0: Initialize the structure with keys in the order givenStep 0: Initialize the structure with keys in the order given

    Step 1: Starting with the last (rightmost) parental node, fixStep 1: Starting with the last (rightmost) parental node, fix

    the heap rooted at it, if it doesnt satisfy the heapthe heap rooted at it, if it doesnt satisfy the heap

    condition: keep exchanging it with its largest childcondition: keep exchanging it with its largest child

    until the heap condition holdsuntil the heap condition holds

    Step 2: Repeat Step 1 for the preceding parental nodeStep 2: Repeat Step 1 for the preceding parental node

    Heap Construction (bottom-up)Heap Construction (bottom-up)

  • 7/31/2019 Depth First Search Alg

    29/44

    Example of Heap ConstructionExample of Heap Construction

    7

    2

    9

    6 5 8

    >

    2

    9

    6 5

    8

    7

    2

    9

    6 5

    8

    7

    2

    9

    6 5

    8

    7

    >

    9

    2

    6 5

    8

    7

    9

    6

    2 5

    8

    7

    >

    Construct a heap for the list 2, 9, 7, 6, 5, 8Construct a heap for the list 2, 9, 7, 6, 5, 8

  • 7/31/2019 Depth First Search Alg

    30/44

    Pseudocode of Bottom-up Heap ConstructionPseudocode of Bottom-up Heap Construction

  • 7/31/2019 Depth First Search Alg

    31/44

    Insertion of a New Element into a HeapInsertion of a New Element into a Heap

    Insert the new element at last position in heap.Insert the new element at last position in heap.

    Compare it with its parent and, if it violates heap condition,Compare it with its parent and, if it violates heap condition,exchange themexchange them

    Continue comparing the new element with nodes up the treeContinue comparing the new element with nodes up the tree

    until the heap condition is satisfieduntil the heap condition is satisfied

    Example: Insert key 10Example: Insert key 10

    Efficiency: O(logEfficiency: O(log nn))

    9

    6

    2 5

    8

    7 1 0

    9

    6

    2 5

    1 0

    7 8

    > >

    1 0

    6

    2 5

    9

    7 8

  • 7/31/2019 Depth First Search Alg

    32/44

    Maximum Key Deletion from a HeapMaximum Key Deletion from a Heap

    Maximum Key Deletion from a HeapMaximum Key Deletion from a Heap

    Step 1Step 1 Exchange the roots key with the last keyExchange the roots key with the last key KKof theof the

    heapheap

    Step 2Step 2 Decrease the heaps size by 1Decrease the heaps size by 1

    Step 3Step 3 Heapify the smaller tree by siftingHeapify the smaller tree by sifting KKdown the treedown the tree

    exactly in the same way we did it in the bottom-up heapexactly in the same way we did it in the bottom-up heap

    construction algorithm.construction algorithm.

    Note: Heapify means to verify the parental dominance forNote: Heapify means to verify the parental dominance for

    KK: if it holds, we are done; if not, swap: if it holds, we are done; if not, swap KKwith the largerwith the larger

    of its children and repeat this operation until the parentalof its children and repeat this operation until the parental

    dominance condition holds fordominance condition holds for KKin its new positionin its new position

  • 7/31/2019 Depth First Search Alg

    33/44

    Example of Maximum Key DeletionExample of Maximum Key Deletion

    Heapso tHeapsort

  • 7/31/2019 Depth First Search Alg

    34/44

    Stage 1: Construct a heap for a given list ofStage 1: Construct a heap for a given list ofnnkeyskeys

    Stage 2: Repeat operation of root removalStage 2: Repeat operation of root removal nn-1 times:-1 times:

    Exchange keys in the root and in the lastExchange keys in the root and in the last

    (rightmost) leaf(rightmost) leaf

    Decrease heap size by 1Decrease heap size by 1 If necessary, swap new root with larger childIf necessary, swap new root with larger child

    until the heap condition holdsuntil the heap condition holds

    HeapsortHeapsort

  • 7/31/2019 Depth First Search Alg

    35/44

    Sort the list 2, 9, 7, 6, 5, 8 by heapsortSort the list 2, 9, 7, 6, 5, 8 by heapsort

    Stage 1 (heap construction)Stage 1 (heap construction) Stage 2 (root/max removal)Stage 2 (root/max removal)

    Example of Sorting by HeapsortExample of Sorting by Heapsort

    Analysis of HeapsortAnalysis of Heapsort

  • 7/31/2019 Depth First Search Alg

    36/44

    Stage 1: Build heap for a given list ofStage 1: Build heap for a given list ofnnkeyskeys

    worst-caseworst-case

    CC((nn) =) =

    Stage 2: Repeat operation of root removalStage 2: Repeat operation of root removal nn-1 times (fix heap)-1 times (fix heap)worst-caseworst-case

    CC((nn) =) =

    Both worst-case and average-case efficiency:Both worst-case and average-case efficiency:

    ((nnloglognn))

    2(2(h-ih-i) 2) 2ii == 2 (2 ( nn log log22((nn + 1))+ 1)) ((nn))ii=0=0

    hh-1-1

    # nodes at

    level i

    i=i=11

    nn-1-1

    2log2log22 ii ((nnloglognn))

    Analysis of HeapsortAnalysis of Heapsort

  • 7/31/2019 Depth First Search Alg

    37/44

    Problem ReductionProblem Reduction

    This variation of transform-and-conquer solves a problem by aThis variation of transform-and-conquer solves a problem by a

    transforming it into different problem for which antransforming it into different problem for which analgorithm is already available.algorithm is already available.

    To be of practical value, the combined time of theTo be of practical value, the combined time of the

    transformation and solving the other problem should betransformation and solving the other problem should besmaller than solving the problem as given by anothersmaller than solving the problem as given by another

    method.method.

  • 7/31/2019 Depth First Search Alg

    38/44

    Examples of Solving Problems by ReductionExamples of Solving Problems by Reduction

    computing lcm(computing lcm(mm,, nn) via computing gcd() via computing gcd(m, nm, n))

    counting number of paths of lengthcounting number of paths of length nnin a graph byin a graph by

    raising the graphs adjacency matrix to theraising the graphs adjacency matrix to the n-n-th powerth power

    transforming a maximization problem to a minimizationtransforming a maximization problem to a minimization

    problem and vice versa (also, min-heap construction)problem and vice versa (also, min-heap construction)

    linear programminglinear programming

    reduction to graph problems (e.g., solving puzzles viareduction to graph problems (e.g., solving puzzles via

    state-space graphs)state-space graphs)

  • 7/31/2019 Depth First Search Alg

    39/44

    Computing the Least Common MultipleComputing the Least Common Multiple

    Traditional approach (middle school method): lcm(Traditional approach (middle school method): lcm(m,nm,n))

    is the product of all the common prime factors ofis the product of all the common prime factors ofmmandandnntimes the product oftimes the product ofmms prime factors that are not ins prime factors that are not in nntimestimes nns prime factors that are not ins prime factors that are not in mm..

    Example: 24 = 2. 2. 2. 3Example: 24 = 2. 2. 2. 3

    60 = 2. 2. 3. 560 = 2. 2. 3. 5lcm(24, 60) =lcm(24, 60) =

    Problem reduction: the greatest common divisor (gcd)Problem reduction: the greatest common divisor (gcd)

    can be efficiently computed (by using Euclids algorithm)can be efficiently computed (by using Euclids algorithm)

    ),gcd(

    .),(nm

    nmnmlcm =

  • 7/31/2019 Depth First Search Alg

    40/44

    Counting Paths in a GraphCounting Paths in a Graph

    Problem: counting paths between two vertices in a graphProblem: counting paths between two vertices in a graph

    Reduction: the number of different paths of length k>0 fromReduction: the number of different paths of length k>0 fromthe ith vertex to the jth vertex of a graph equals the (I, j)ththe ith vertex to the jth vertex of a graph equals the (I, j)th

    element of where A is the adjacency matrix of the graph.element of where A is the adjacency matrix of the graph.k

    A

  • 7/31/2019 Depth First Search Alg

    41/44

    Reduction of Optimization ProblemsReduction of Optimization Problems

    Maximization problem: find a maximum of some functionMaximization problem: find a maximum of some function

    Minimization problem: find a functions minimumMinimization problem: find a functions minimum

    minminff(x) = -(x) = - [-[- ff(x)](x)]

    maxmaxff(x) = -(x) = - minmin[-[- ff(x)](x)]

  • 7/31/2019 Depth First Search Alg

    42/44

    Linear ProgrammingLinear Programming

    Many problems of optimal decision making can beMany problems of optimal decision making can be

    reduced to an instance of the linear programming, whichreduced to an instance of the linear programming, whichis a problem of optimizing a linear function of severalis a problem of optimizing a linear function of severalvariables subject to constraints in the form of linearvariables subject to constraints in the form of linearequations and linear inequalities.equations and linear inequalities.

    Example: Consider a university endowment that needs toExample: Consider a university endowment that needs to

    invest $100 million, which must be split between stocks,invest $100 million, which must be split between stocks,bonds, and cash. The expected annual return of stock,bonds, and cash. The expected annual return of stock,bond and cash will be 10%, 7%, and 3% respectively.bond and cash will be 10%, 7%, and 3% respectively.The amount invested in stocks must be no more than oneThe amount invested in stocks must be no more than onethird of the moneys invested in bonds. In addition, atthird of the moneys invested in bonds. In addition, at

    least 25% of the total amount invested in stocks andleast 25% of the total amount invested in stocks andbonds must be invested in cash. How should thebonds must be invested in cash. How should themanagers invest the money to maximize the return?managers invest the money to maximize the return?

  • 7/31/2019 Depth First Search Alg

    43/44

    Reduction to Graph ProblemsReduction to Graph Problems

    Many problems, especially puzzles and games, can beMany problems, especially puzzles and games, can be

    reduced to the standard graph problems.reduced to the standard graph problems.

    State-space graph: vertices typically represent possibleState-space graph: vertices typically represent possible

    states of the problem in question while edges indicatestates of the problem in question while edges indicate

    permitted transitions among such states.permitted transitions among such states.

    Reduction: a path from the initial-state vertex to a goal-Reduction: a path from the initial-state vertex to a goal-

    state vertex.state vertex.

    Example (river-crossing puzzle): A peasant finds himself onExample (river-crossing puzzle): A peasant finds himself on

    a river bank with a wolf, a goat, and a head of cabbage. Hea river bank with a wolf, a goat, and a head of cabbage. He

    needs to transport all three to the other side of the river inneeds to transport all three to the other side of the river in

    his boat. However, the boat has room only for the peasanthis boat. However, the boat has room only for the peasant

    himself and one other item. In his absence, the wolf wouldhimself and one other item. In his absence, the wolf would

    eat the goat, and the goat would eat the cabbage.eat the goat, and the goat would eat the cabbage.

    State space graph for the river crossing puzzleState space graph for the river crossing puzzle

  • 7/31/2019 Depth First Search Alg

    44/44

    State-space graph for the river-crossing puzzleState-space graph for the river-crossing puzzle