KonkurStudy
همه دروس

طراحی الگوریتمAlgorithms

روش‌های طراحی الگوریتم، مرتب‌سازی، گراف‌ها، برنامه‌ریزی پویا، الگوریتم‌های حریصانه و پیچیدگی.

کنکور ارشد علوم کامپیوتر

134 سوالات46 کارت‌ها8 مباحث

مباحث

کارت‌ها · 46

مفهومطراحی الگوریتم
Insertion Sort on Nearly-Sorted Arrays

Insertion sort is adaptive: it runs in Θ(n) on nearly-sorted input where merge/quicksort still pay Θ(nlog n) .

insertion-sortadaptive-sortingnearly-sortedcomparison-count
الگوریتمطراحی الگوریتم
All-Pairs Shortest Path via Floyd-Warshall

Floyd-Warshall is the dynamic-programming algorithm that finds shortest paths between every pair of vertices in Θ(n^3) .

all-pairs-shortest-pathfloyd-warshalldynamic-programminggraph-algorithms
مفهومطراحی الگوریتم
Asymptotic Notation (O, Omega, Theta) Manipulation Rules

Adding terms of lower-or-equal order keeps Theta; the largest term dominates, and identities can break when f is not assumed to grow.

asymptotic-notationbig-thetabig-omegabig-o
مفهومطراحی الگوریتم
Backtracking vs. Branch and Bound in State-Space Search

Backtracking suits feasibility/constraint-satisfaction searches; Branch and Bound adds a cost bound for optimization.

backtrackingbranch-and-boundstate-space-treen-queens
مفهومطراحی الگوریتم
BFS Scans Each Undirected Edge Exactly Twice

On adjacency lists, BFS touches every undirected edge twice (once per endpoint), giving total work Θ(V+E) .

bfsgraph-traversaladjacency-listrunning-time
مفهومطراحی الگوریتم
Dijkstra Correctness Conditions

Dijkstra is correct iff all edge weights are non-negative; cycles (even directed) are irrelevant.

dijkstrashortest-pathsnon-negative-weightsgreedy
نکتهطراحی الگوریتم
Dijkstra Requires Non-Negative Edge Weights

Dijkstra is correct only when all edge weights are non-negative; 'no negative cycle' is not enough.

dijkstrashortest-pathsnegative-weightsgreedy
مفهومطراحی الگوریتم
Huffman Coding: Prefix Codes, Optimality & k-ary Merge

Huffman greedily merges the smallest weights into an optimal prefix-free tree; the same rule gives k-ary (e.g. ternary) Huffman and optimal-merge patterns whose cost is the weighted path length.

huffman-codingprefix-codegreedyoptimal-merge-pattern
مفهومطراحی الگوریتم
k-Way Merge Time Complexity

Merging k sorted lists with n total elements costs Θ(nlog k) , which is O(n) only when k is constant.

k-way-mergemergemin-heaplower-bound
الگوریتمطراحی الگوریتم
Kruskal's Algorithm: Cycle-Creating Edges Are Rejected

Kruskal sorts edges by weight and accepts an edge only if its endpoints lie in different components; otherwise it would close a cycle and is rejected.

kruskalminimum-spanning-treeunion-findcycle-detection
قضیهطراحی الگوریتم
Master Theorem Case 3 (work-dominated recurrence)

When the per-call work f(n) outgrows the watershed n^(log_b a), the recurrence is dominated by the top level and T(n)=Theta(f(n)).

master-theoremrecurrenceasymptotic-analysisdivide-and-conquer
فرمولطراحی الگوریتم
Median-Pivot Quicksort Recurrence

Picking the exact median as pivot gives a perfectly balanced split, yielding T(n)=2T(n/2)+f(n)+Θ(n)=O((n+f(n))log n) .

quicksortmedianpivot-selectionrecurrence
الگوریتمطراحی الگوریتم
Minimum Spanning Tree (Kruskal/Prim)

Greedily build a cheapest set of edges that connects all vertices with no cycle, using exactly n-1 edges.

minimum-spanning-treekruskalprimgreedy
مفهومطراحی الگوریتم
Output-size lower bound forces exponential enumeration

An algorithm cannot run faster than the size of the output it must produce, so enumerating exponentially many objects takes exponential time.

output-sensitiveexponentialpath-enumerationlower-bound
فرمولطراحی الگوریتم
Element Contribution Across All Subsets

Each element of an n-set sits in exactly 2^(n-1) subsets, so the total of all subset-sums is 2^(n-1) times the element sum.

combinatoricssubsetscountingpower-set
مفهومطراحی الگوریتم
Subtract-and-Conquer Recurrence Growth

When the argument shrinks by a constant (n-1) instead of a factor, unroll the recurrence; a branching factor a>1 gives exponential growth Θ(a^n) .

recurrence-relationasymptotic-analysissubtract-and-conquerexponential-growth
مفهومطراحی الگوریتم
Interpreting Worst-Case Upper Bounds

A worst-case upper bound ("runs in at most ...") limits the slowest run but says nothing about average case or behavior on a specific input.

worst-caseupper-boundsasymptotic-analysisgrowth-rates
مفهومطراحی الگوریتم
Approximation Algorithms and Approximation Ratio

For an NP-hard optimization problem, a polynomial-time rho-approximation guarantees a solution within a factor rho of the optimum; the ratio is a monotone worst-case bound (factor c gives every c' >= c for free) and is defined only for optimization, never for decision, problems.

approximation-algorithmsapproximation-rationp-hardvertex-cover
مفهومطراحی الگوریتم
Dynamic Programming: Optimal Substructure & Overlapping Subproblems

DP applies when an optimal solution is built from optimal solutions of overlapping subproblems; pick a state, write a recurrence, and evaluate it bottom-up (tabulation) or top-down (memoization).

dynamic-programmingoptimal-substructureoverlapping-subproblemsmemoization
قضیهطراحی الگوریتم
Max-Flow Min-Cut Theorem

In any flow network the value of a maximum s - t flow equals the capacity of a minimum s - t cut; a flow is maximum exactly when its residual graph has no augmenting path, which is how Ford-Fulkerson finds it.

max-flowmin-cutmax-flow-min-cut-theoremnetwork-flow
مفهومطراحی الگوریتم
NP-Completeness, NP-Hardness and Reductions

A problem is NP-complete when it is both in NP and NP-hard; under the assumption P ≠ NP such problems have no polynomial-time algorithm, and we prove hardness by a polynomial-time reduction from a known hard problem like SAT, Knapsack, or TSP.

np-completenessnp-hardnessp-vs-nppolynomial-reduction
الگوریتمطراحی الگوریتم
BFS/DFS Visit Order with Tie-Breaking

Trace BFS (queue) and DFS (stack/recursion) deterministically by breaking every tie in alphabetical order — and read off the first/last vertex visited.

bfsdfsgraph-traversaltie-breaking
الگوریتمطراحی الگوریتم
Dijkstra's Extract-Min and Edge Relaxation Order

Dijkstra finalizes vertices in increasing distance order; each time one is extracted, it relaxes all of that vertex's outgoing edges — so the edge-relaxation sequence is dictated by the extract-min order.

dijkstrashortest-pathsrelaxationextract-min
مفهومطراحی الگوریتم
Order of a Loop's Accumulated Output (Summation Magnitude)

Find the order of the VALUE a loop prints — not its running time — by turning the nested loops into a closed-form sum or product and reading off its leading power.

complexitysummationasymptoticspower-sums
مفهومطراحی الگوریتم
Formal Definition of Class P and Quantifier Order

P is the set of languages decided by ONE algorithm in ONE fixed polynomial time bound for ALL inputs — the order ∃A ∃c ∀x is what makes it polynomial.

complexityclass-ppolynomial-timequantifiers
مفهومطراحی الگوریتم
Counting Quicksort Comparisons on a Fixed-Size Array

On an array of size n, Quicksort's comparison count is a random quantity ranging from nlog n (best) to n(n-1)/2 (worst), determined entirely by how each pivot splits its subarray.

quicksortcomparison-countpartitionrecurrence
مفهومطراحی الگوریتم
Verifying a Binary-Search Probe Sequence in Linear Time

Checking whether a claimed list of probes is the exact sequence a binary search would make costs only Θ(n) — replay the deterministic search and compare, never re-sort or re-search.

binary-searchverificationdeterministic-replaylinear-time
الگوریتمطراحی الگوریتم
Bellman-Ford Relaxation Loop and Cycle Detection

Bellman-Ford relaxes every edge n-1 times to settle all shortest paths; a single extra (n-th) pass that still relaxes an edge proves a negative cycle is reachable.

bellman-fordshortest-pathsnegative-cyclecycle-detection
قضیهطراحی الگوریتم
Bubble Sort Swap Count Equals Inversions

Bubble sort (and any sort that only swaps adjacent out-of-order elements) performs exactly as many swaps as there are inversions in the input — so you can count swaps without simulating the passes.

bubble-sortinversionsswap-countsorting
مفهومطراحی الگوریتم
Time Complexity of Elementary Sorts (Bubble vs. Selection)

Bubble, selection, and insertion sort all run in Theta(n^2) on a random array; what distinguishes them is whether their cost depends on the input — selection sort is always Theta(n^2), bubble and insertion can drop to Theta(n) on sorted data.

sortingbubble-sortselection-sortinsertion-sort
الگوریتمطراحی الگوریتم
Dutch National Flag Three-Way Partition

Sort an array of three distinct key values into low-mid-high order in a single linear, in-place pass using three pointers.

dutch-national-flagthree-way-partitionin-placetwo-pointer
الگوریتمطراحی الگوریتم
Exponentiation by Squaring (computing x^n in Theta(log n))

Computing x^n with the four basic operations needs only Θ(log n) multiplications, not Θ(n) : repeatedly square the base while halving the exponent, multiplying in an extra x whenever a bit of n is 1 .

exponentiation-by-squaringfast-powerdivide-and-conquerbinary-exponentiation
نکتهطراحی الگوریتم
Greedy Coin Change and Non-Canonical Coin Systems

The greedy "take the largest coin that fits" rule for making change is not always optimal — it works only for canonical denomination systems. On a non-canonical set like 10,7,2,1 , greedy can use more coins than the true minimum.

greedycoin-changechange-makingcanonical-coin-system
مفهومطراحی الگوریتم
Ordering Functions by Growth Rate (log, constants in disguise)

To rank functions slowest-to-fastest, first unmask the impostors: rewrite each as a power of 2 via n=2^(log n) . Some "functions" are secretly constants, and log^()n grows slower than any iterated logarithm.

growth-rateasymptoticsiterated-logarithmlog-star
مفهومطراحی الگوریتم
Loop Running Time as a Sum of Per-Iteration Work

The cost of a loop is the SUM of the work each iteration does, not (iterations) x (max cost). When the body costs a constant, the sum collapses to (count) x O(1); when the body cost varies with i, evaluate the series. Split a loop with several statements into separate sums and add them.

loopssummationtime-complexityper-iteration-cost
مفهومطراحی الگوریتم
Loop Iteration Count from the Update Rule (i+1 vs i2 vs ii)

The cost of a single counted loop is set by how the counter is UPDATED, not how it is compared. Solve the recurrence on the counter, set it equal to the bound n, and invert: i+=c gives n/c steps, i=c gives log_c n steps, and i=ii gives log log n steps.

loopsiteration-counttime-complexityasymptotics
قضیهطراحی الگوریتم
Master Theorem Case 2 (balanced recurrence, the merge-sort case)

When the work to split/combine, f(n) , exactly matches the leaf cost n^(log_b a) , the recurrence T(n)=aT(n/b)+f(n) solves to Θ (n^(log_b a)log n) — every level costs the same, and there are Θ(log n) levels.

master-theoremcase-2divide-and-conquerrecurrence
مفهومطراحی الگوریتم
Counting Merge-Sort Calls, Merges, and Comparisons

Model one run of merge sort as a recursion tree with n leaves: there are n-1 internal (splitting) nodes, so merge is called n-1 times, total mergesort invocations are 2n-1, and the worst-case element comparisons are at most nceil(log2 n) - 2^ceil(log2 n) + 1. Don't confuse 'total calls' with 'recursive calls'.

merge-sortsortingrecursion-treecomparison-counting
مفهومطراحی الگوریتم
Merge Sort with a Constant-Size Insertion-Sort Cutoff

Stopping merge-sort recursion at blocks of a constant size k and finishing each block with insertion sort keeps the running time at Θ(nlog n) ; the cutoff only shaves a constant factor. The bound changes only when k is allowed to grow with n .

merge-sortinsertion-sortcutoffcoarsening
مفهومطراحی الگوریتم
Nested Self-Referential Recurrences (Hofstadter G-Sequence)

When the recurrence feeds its own output back into itself, like G(n)=n-G(G(n-1)), unroll a few terms and look for a golden-ratio floor closed form.

recurrenceself-referentialhofstadtergolden-ratio
الگوریتمطراحی الگوریتم
Solving Recurrences by Change of Variable (n = 2^m)

When a recurrence shrinks n by a constant factor (e.g. n→ n/2 or n→√n ), the substitution n=2^m converts division into subtraction, turning a hard recurrence in n into an easy one in m that you can unroll or hit with the Master Theorem.

change-of-variablesubstitutionrecurrencedivide-and-conquer
الگوریتمطراحی الگوریتم
Binary Search on a Rotated Sorted Array

A sorted array cyclically rotated by some unknown amount still hides enough order to binary-search it in Θ(log n) : there is exactly one "drop" where A[i]>A[i+1] , and at every midpoint one half is guaranteed sorted, so you can find the pivot, the minimum, the maximum, or any target in logarithmic time.

rotated-sorted-arraybinary-searchpivot-searchfind-minimum
الگوریتمطراحی الگوریتم
Matrix-Chain Multiplication Ordering

Matrix multiplication is associative but its scalar-multiplication cost is not, so dynamic programming over interval splits finds the cheapest parenthesization in Θ(n^3) time.

matrix-chainparenthesizationinterval-dpdynamic-programming
مفهومطراحی الگوریتم
Radix Sort and the Stable-Subroutine Requirement

LSD radix sort is correct only if the per-digit auxiliary sort is stable, so heapsort and quicksort cannot be plugged in while stable counting sort can.

radix-sortstable-sortauxiliary-sortcounting
الگوریتمطراحی الگوریتم
Fractional Knapsack (Greedy by Value Density)

Sort items by value-to-weight ratio and fill the knapsack greedily, taking a fraction of the last item; this is provably optimal for the fractional knapsack, unlike 0/1.

greedyfractional-knapsackvalue-densityprofit-to-weight-ratio
الگوریتمطراحی الگوریتم
Job Sequencing with Deadlines (Greedy on a Matroid)

Schedule unit-time jobs for maximum profit by taking them in decreasing-profit order and placing each in the latest free slot before its deadline — provably optimal because the feasible sets form a matroid.

greedyjob-sequencingdeadlinesscheduling

سوالات · 30/134

همهٔ 134 سؤال →