Algorithms

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Topological Sort Algorithm

1. Store each vertex's in-degree in an array D. 2. Initialize queue with all "in-degree = 0" vertices 3.While there are vertices remaining in the queue: dequeue and output a vertex, reduce in degree of all vertices adjacent to it by 1, enqueue any of these vertices whose in-degree became zero. 4. If all vertices are output then success, otherwise there is a cycle.

Directed Graph

A diagram consisting of vertices, joined by directed edges.

Spanning Tree

A subgraph of a connected graph that is a tree and includes all the vertices of the original graph.

Implementation

Assume adjacency list representation

Checking if 2 vertices are connected or not

Best solution is disjoint sets: sets whose intersection is the empty set so it means that they don't have any element in common.

Depth First Search

Dig deeper on one neighbor before knowing any other neighbors. Recursive algorithm that uses the idea of backtracking.Implemented using stacks. Steps: 1. Pick a starting node and push all its adjacent nodes into a stack. 2.Pop a node from stack to select the next node to visit and push all its adjacent nodes into a stack. 3. Repeat this process until the stack is empty. However, ensure that the nodes that are visited are marked. This will prevent you from visiting the same node more than once. 4. If you fo not mark the nodes that are visited and you visit the same node more than once you may end up in an infinite loop

Minimum Spanning Tree

Given an undirected and connected graph G=(V,E) a spanning tree of the graph G is a tree that spans G (that is, it includes every vertex of G) and is a subgraph of G (every edge in the tree belongs to G) 1. Minimum spanning tree has direct application in the design of networks.2. It is used in algorithms approximating the travelling salesman problem, multi-terminal minimum cut problem and minimum-cost weighted perfect matching. 3. Cluster Analysis4. Handwriting recognition5. Image segmentation

Edge(arc or link)

Gives the relationship between two vertices

Prim's Algorithm

Greedy approach to find the minimum spanning tree. Grow the MST from a starting position and add vertex to the growing spanning tree instead of edge.

Connected Components using DFS

In an undirected graph, a connected component is a set of vertices in a graph that are linked to each other by paths.

Topological Sort Analysis

Initialize in -degree array: O(|V| + |E|) Initialize queue with in-degree 0 vertices: O(|V|) Dequeue and output vertex: |V| vertices, each takes only O(1) to dequeue and output: O(|V|) Reduce in-degree of all vertices adjacent to a vertex and enqueue any in-degree 0 vertices: O(|E|) For input graph G = (V, E) run time = O(|V| + |E|) - Linear time!

Maintaining Degree 0 Vertices

Key idea: Initialize and maintain a queue(or stack) of vertices with in-degree 0.

Breath First Search for Graph

Know all your immediate neighbors first before knowing any other neighbors know closer neighbors first before knowing further neighbors. During traversal, it is important that you track which vertices have been visited. The most common way of tracking vertices is to mark them. Use a queue and a visited array. When queue is empty end the loop.

Topological Sort

Linear ordering of the vertices of a directed graph such that for every directed edge "uv" which connects "u" to "v" (u points to v), u comes before v. This ordering is only possible if and only if there are no directed cycles in the graph, therefore, it must be a DAG.

Steps to Prim's Algorithm

Maintain two disjoint sets of vertices. One containing vertices that are in the growing spanning tree and other that are not in the growing spanning tree. Select the cheapest vertex that is connected to the growing spanning tree and is not in the growing spanning tree and add it into the growing spanning tree. • This can be done using Priority Queues .• Insert the vertices, that are connected to growing spanning tree, into the Priority Queue. Check for cycles. To do that, mark the nodes which have been already selected and insert only those nodes in the Priority Queue that are not marked.

Graph definition

Non-linear data structure Consists of a set of vertices and a set of edges that relate the vertices to each other. (set of edges describes the relationship among the vertices) Represent them as adjacency lists and adjacency matrices

Time Complexity of DFS and BFS

O(V + E) : vertices + edges. The first for-loop is executed at most |V| times. The reason is that every vertex is checked. So, we have O(V). The second for loop is executed at most |E| times if G is a directed graph or 2|E| times if G is undirected. So, we have O(E).

Adjacency Matrix complexities

See photo

Adjacency list: complexities

See photo

Kruskals Algorithm Steps

Sort the graph edges with respect to their weights Start adding edges to the MST from the edge with the smallest weight until the edge of the largest weight. Only add edges which doesn't form a cycle, edges which connect only disconnected components.

Adjacency Matrix

a representation of a graph in which a boolean matrix contains a 1 at position (i,j) if there is an arc from node i to node j.

Adjacency List

a representation of a graph in which each node has a list of nodes that are adjacent to it, i.e. connected to it by an arc.

Kruskal's Algorithm

builds the spanning tree by adding edges one by one into a growing spanning tree Follows the greedy approach as in each iteration it finds an edge which has least weight and add it to the growing spanning tree.

Undirected Graph

describes a graph in which the edges may be followed in either direction.

Vertex(node or point)

fundamental unit out of which graphs are formed

When to use Adjacency Matrix

preferred when the graph is dense or we need to tell quickly if there's an edge connecting two given vertices. Weighted graph: A[i,j] will be the weight w(i,j) if (i,j) is in E

When to use Adjacency List

usually the method of choice, as most graphs are sparse. Weighted graphs can also be represented with adjacency list. Adj[i] will point to the head of a linked list of nodes each of which has two properties: vertex j and weight w(i,j) is (i,j) is in E.


Set pelajaran terkait

Quiz 2 Tissue Integrity & Gas Exchange

View Set

Quiz 09: Sale Comparison Approach to Value

View Set

Chapter 5 Test your understanding

View Set

Chapter 5: Developing Leadership Skills

View Set

Identify the work and author (2nd semester NA1)

View Set

Ohio Laws and Rules (Chapter 10)

View Set