본문 바로가기

Algorithm

(8)
Prim’s Algorithm Prim’s Algorithm is another approach to find the minimum spanning tree (MST) from a given graph.Its code implementation is simpler and easier than Kruscal’s Algorithm based on union-find.Key IdeaGrow the MST greedily: Pick the smallest edge that connects a visited node to an unvisited node.Priority queue (min-heap): Use min-heap to efficiently pick the smallest edge.Repeat until all nodes are in..
Kruskal’s Algorithm GoalOur goal is to find the minimum spanning tree (MST) from the graph, which satisfies:All the nodes are connected.The sum of weights is minimized.IdeaThe key ideas for the Kruskal’s algorithm are:Greedy approach: we always process the edge with smallest weight first.Sort the edges in ascending order before processing.Exclude redundant edges: if adding the edge forms a cycle, skip it.Use union-..
Topological Sorting The figure on the left is a directed acyclic graph (DAG) representation for the following table, which contains information of dependencies between cources.No.Course NamePrerequisitesNumber of Prerequisites1Calculus-02Linear Algebra-03Probability TheoryCalculus14Information TheoryProbability Theory15Vector CalculusCalculus, Linear Algebra26Mathematical StatisticsProbability Theory, Information T..
Breadth-Frist Search (BFS) vs Depth-First Search (DFS) When exploring all nodes in a graph, we have two fundamental traversal methods:Breadth-First Search (BFS)Depth-First Search (DFS)Breadth-Frist Search (BFS)Key IdeaBFS explores nodes level by level, meaning it visits all neighbors of a node before moving deeper. This ensures that nodes are processed in increasing order of distance from the starting node.Steps:Start from a given node.Visit all its..
Priority Queue, Heap, and Complete Binary Tree Three-Line Summary!Priority queue: a data structure where elements have priorities.Heap: a special case of a CBT, used for implementation of a priority queue.Complete binary tree (CBT): a special case of a binary tree.Binary TreeA binary tree is a hierarchical data structure where each node can have at most two children.The two children are called the left child and the right child.If a binary t..
Stack vs Queue CommonalityStack and Queue, both store data in a linear order, i.e., elements are arranged sequentially.Difference StackQueueOrderLast In, First Out (LIFO)First In, First Out (FIFO)Insertionpush()(adds element at the top)enqueue()(adds element at the rear)Deletionpop() (removes element from the top)dequeue() (removes element from the front) The key difference is their processing order—LIFO and F..
Union-Find Algorithm Find RootThis tree graph can be described by a parent table.parent = [0, 1, 1, 1, 3, 3, 6, 6], representing the parent node for each node. (including node 0, although it is not used.)parent[1] = 1, parent[6] = 6. They are roots of the other nodes, since their parents are themselves.parent[2] = parent[3] = 1, parent[4] = parent[5] = 3, parent[7] = 6.The root of each node can be found using the pa..
Dijkstra’s Algorithm Our goal is to find the length of the shortest path to reach each node from the given start node.Use DP (Dynamic Programming) table to record and update the shortest path and its length.Key IdeaExamine outgoing edges from the start node, visit the nearest node.For example, start = A, nearest = CGreedy selection. Examine outgoing edges from that nearest node. Compare the original path and the new..