Backtracking.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Graph & BFS.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Homework #5 Due: October 31, 2000 Christine Kang Graph Concepts and Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs and Paths : Chapter 15 Saurav Karmakar
Great Theoretical Ideas in Computer Science for Some.
Graphs and Shortest Paths Using ADTs and generic programming.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs and Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures Graphs - Terminology
Basic Concepts Graphs For more notes and topics visit:
Fundamentals, Terminology, Traversal, Algorithms
CMSC 341 Graphs.
Discrete Mathematicsq
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
CISC 235: Topic 10 Graph Algorithms.
Depth-First Search.
CS120 Graphs.
More Graph Algorithms.
Spanning Trees Longin Jan Latecki Temple University based on slides by
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Graph & BFS.
Graphs Chapter 13.
"Learning how to learn is life's most important skill. " - Tony Buzan
Minimum Spanning Trees
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search Graph Traversals Depth-First Search DFS.
Richard Anderson Autumn 2016 Lecture 5
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Backtracking

General algorithm bool finished = FALSE; backtrack(int a[], int k, data input){ int c[MAXCANDIDATES],ncandidates,i; if (is_a_solution(a,k,input) process_solution(a,k,input); else { k = k+1; construct_candidates(a,k,input,c,&ncandidates); for (i=0; i<ncandidates;i++){ a[k] = c[i]; backtrack(a,k,input); if (finished ) return; }

Pruning Process of not considering cases because they cannot be solutions. Very problem specific.

Graph Traversals

Graph flavors Undirected vs directed Weighted vs unweighted Cyclic vs acyclic Simple vs non-simple (self-loops, multi-edges) Embedded vs topological Implicit vs explicit Labeled vs unlabeled

Data Structures for Graphs Adjacency matrix List of adjacency lists Array/vector of adjacency lists List or table of edges.

Graph traversals, basic algorithm: traverse_graph(graph g, node start){ Structure s; Node v; Node visited[]=false; add(s,start); While ( !empty s){ v=get1(s) If ! visited[v] { visit(v) visited[v] = true; foreach x=neighbor(v) add(s,x) }

Finding cycles Dfs a discovery is a cycle.

Algorithm for Topological sort Ideal data structure: array/vector of adjacency lists. vector pred_count[]=0; Step 1: Go through each adjacency list, and add one to the the pred_count entry of the node listed. Repeat until none found: Go through pred_count vector until a node with 0 predecessors is found; output it, subtract 1 from each of its neighbors. If there are nodes left to output, there is a cycle and no topological sort possible, ow. We are done.

More graph algorithms

Some more concepts Degree of a node: number of edges it “touches” Indegree: edges pointed to it; Outdegree: edges pointed from it. Note that the sum of the degree = |edges| *2 Therefore, nodes with odd degree is even. Graphs which “can be drawn without lifting the pen from the paper” have at most two nodes of odd degree. Also indegrees = outdegrees.

Even more concepts Trees: Undirected graphs without cycles. Rooted trees: Direted graphs where oevery node but 1 has indegree 1 Binary tree: