Graphs and Algorithms

Similar documents
CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

CS 206 Introduction to Computer Science II

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Understand graph terminology Implement graphs using

PATH FINDING AND GRAPH TRAVERSAL

LECTURE 17 GRAPH TRAVERSALS

CSCE 350: Chin-Tser Huang. University of South Carolina

Module 11: Additional Topics Graph Theory and Applications

Lecture 3. Brute Force

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Some major graph problems

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Graphs: Graph Data Structure:

Graph. Vertex. edge. Directed Graph. Undirected Graph

Chapter 14 Section 3 - Slide 1

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Introduction III. Graphs. Motivations I. Introduction IV

Graphs Data Structures

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

An undirected graph G can be represented by G = (V, E), where

Ma/CS 6a Class 8: Eulerian Cycles

Outline. Graphs. Divide and Conquer.

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Topics Covered. Introduction to Graphs Euler s Theorem Hamiltonian Circuits The Traveling Salesman Problem Trees and Kruskal s Algorithm

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Lecture 26: Graphs: Traversal (Part 1)

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?

Graph Search Methods. Graph Search Methods

Trees. Arash Rafiey. 20 October, 2015

CSS 343 Data Structures, Algorithms, and Discrete Math II. Graphs II. Yusuf Pisan

Fundamental Algorithms

Graph Search Methods. Graph Search Methods

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

GRAPHS Lecture 17 CS2110 Spring 2014

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Inf 2B: Graphs, BFS, DFS

DESIGN AND ANALYSIS OF ALGORITHMS

1. The Highway Inspector s Problem

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

CS 310 Advanced Data Structures and Algorithms

CS/COE

SEVENTH EDITION and EXPANDED SEVENTH EDITION

I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

CS302 - Data Structures using C++

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Algorithms: Lecture 10. Chalmers University of Technology

Graphs. The ultimate data structure. graphs 1

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

EECS 203 Lecture 20. More Graphs

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Logic: The Big Picture. Axiomatizing Arithmetic. Tautologies and Valid Arguments. Graphs and Trees


Exercises for Discrete Maths

The Bridges of Konigsberg Problem Can you walk around the town crossing each bridge only once?

Sections 5.2, 5.3. & 5.4

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Computer Science and Software Engineering University of Wisconsin - Platteville. 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville

Brute Force: Selection Sort

Graph Traversals BFS & DFS 1 CS S-16

The Traveling Salesman Problem

Lecture 24 Notes Search in Graphs

State Space Search. Many problems can be represented as a set of states and a set of rules of how one state is transformed to another.

MATH 113 Section 9.2: Topology

CSE 100: GRAPH ALGORITHMS

IE 102 Spring Routing Through Networks - 1

The Konigsberg Bridge Problem

Introduction to Computer Science and Programming for Astronomers

Chapter 8 Topics in Graph Theory

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao,David Tse Note 8

Intermediate Math Circles Wednesday, February 22, 2017 Graph Theory III

Module 5 Graph Algorithms

6.2. Paths and Cycles

The Traveling Salesman Problem Outline/learning Objectives The Traveling Salesman Problem

Study Guide Mods: Date:

V1.0: Seth Gilbert, V1.1: Steven Halim August 30, Abstract. d(e), and we assume that the distance function is non-negative (i.e., d(x, y) 0).

Solutions to relevant spring 2000 exam problems

Circuits and Paths. April 13, 2014

Graph theory was invented by a mathematician named Euler in the 18th century. We will see some of the problems which motivated its study.

CSI 604 Elementary Graph Algorithms

Number Theory and Graph Theory

Graphs Introduction and Depth first algorithm

The Traveling Salesman Problem Outline/learning Objectives The Traveling Salesman Problem

Assignment No 2 (Group B)

Analysis of Algorithms

CSE 100: GRAPH SEARCH

Chapter 28. Graphs and Applications. Part1. Non-weighted Graphs

Introduction to Graphs

Branch and Bound. Live-node: A node that has not been expanded. It is similar to backtracking technique but uses BFS-like search.

CS2 Algorithms and Data Structures Note 9

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Excursions in Modern Mathematics Sixth Edition. Chapter 5 Euler Circuits. The Circuit Comes to Town. Peter Tannenbaum

Graph: representation and traversal

Graphs: A graph is a data structure that has two types of elements, vertices and edges.

Transcription:

Graphs and Algorithms Graphs are a mathematical concept readily adapted into computer programming. Graphs are not just data structures, that is, they are not solutions to simple data storage problems. Graphs are usually used because of the algorithms associated with them. Apr 19 1

Introduction to Graphs Graphs are rather like trees. From a math perspective, a tree is a graph, but there are many graphs that are not trees. From a programming perspective, graphs are used differently from trees. Usually used to represent data that has a physical existence. Apr 19 2

Definitions A graph consists of vertices and edges. Two nodes are said to be adjacent if they are connected by a single edge. A path is a sequence of edges. A graph is said to be connected if there is at least on path from every vertex to every other. Apr 19 3

Directed and Weighted Graphs These are graphs produced from basic graphs by introducing edge variations. Directed graphs add directions to the edges, that is, one can only travel one way along the edges. Weighted graphs add weights to the edges, often to represent physical distances between the vertices. Apr 19 4

The Seven Bridges of Koenigsberg The is the first well known application of the graphs. Leonhard Euler, in the early 1700s. Apr 19 5

Representing Graphs in a Program Unlike trees, whose data is only stored at the nodes (vertices), a graph can store data both at the vertices and at the edges. Also unlike binary trees, a graph may have any number of adjacent vertices. Apr 19 6

Vertices The class usually stores an object representing all pertinent data of the physical object. Also often stores a flag/counter used by searching/traversing algorithms. All vertices are usually placed in arrays, and referred to using their index number. Apr 19 7

Vertex class class Vertex { public City c; public char label; public boolean visited; } public Vertex (City new, char l) { c = new; label = l; visited = false; } Apr 19 8

Edges The solution to the freeform connections in graphs is to use either an adjacent matrix or an adjacent list. An adjacent matrix is a two dimensional array in which the elements indicate whether an edge is present between tow vertices. Apr 19 9

Apr 19 10 The Adjacent Matrix If a graph has n vertices, the adjacent matrix will be of size nxn. The existence of an edge is indicated by a 1, and 0 otherwise. 0 0 1 1 D 0 0 0 1 C 1 0 0 1 B 1 1 1 0 A D C B A

The Adjacency List Use a linked list to indicate all the vertices adjacent to a particular vertex. Need one linked list per vertex. vertex A B C D Adjacency List BCD AD A AB Apr 19 11

Adding Vertices and Edges to a Graph For each vertex: make a new vertex object and insert it into the vertex array vertexlist. vertexlist[n++] = new Vertex(NewYork, F ); Add all edges connecting to the new vertex into the adjacency matrix or list. adjmat[1][n 1] = 1; adjmat[n 1][1] = 1; lists[n 1].append(1); lists[1].append(n 1); Apr 19 12

Searches Finding which vertices can be reached from a specified vertex. One of the most fundatmental operations on a graph. An algorithm which starts at any specified vertex, systematically moves along edges to other vertices such that when it s done, you are guaranteed to have visited every connected vertex. Apr 19 13

Depth First Search As the name suggests, the algorithm will try to go down (to an adjacent vertex) for as far as possible, and then back track. This is especially obvious when the graph is indeed a tree (which then has a clear updown relationship between the vertices). It is implemented using a stack to remember where it was when a dead end is reached. Apr 19 14

Example What is the depth first traversal order A B C D E F G H I J K Apr 19 15

The DFS Algorithm If possible, visit an adjacent unvisited vertex, mark it, then push it onto the stack. If you can t follow rule 1, then if possible, pop a vertex off the stack. If you can t follow rule 1 or rule 2, you are done. Apr 19 16

Breadth First Search Instead of trying to get as far away from the start point, we can also try to stay as close as possible. In tree terminology, we want to visit all nodes level by level. BFS for the previous example?? BFS is implemented with a queue. Apr 19 17

The BFS Algorithm Make start vertex current vertex, visit it. If possible, visit the next unvisited vertex that is adjacent to the current one, mark it and insert it into the queue. If you can t carry out rule 1, remove a vertex from the queue and make it the current vertex. If you can t carry out rule 1 or 2, you are Apr 19 18

Classic Graph Problems The 7 bridges of Koenigsberg Problem Is there a path by which I can cross every bridge exactly once? More generally, given a graph is there a path on on which I can traverse every EDGE once Also, is there a path on which I can visit every vertex once Traveling Salesman problem Given a graph with weighted edges, what is the path with minimum weight that visits every Apr 19 19

Brute Force vs Analytic Solutions Every solvable graph theory problem has a brute force solution Problem, brute force could take a long time TSP with 100 fully connected cities srequires considering about 2 100 paths http://www.tsp.gatech.edu/cpapp/index.html Sometimes there are anlaytic soltions 7 bridges of Koenigsberg Problem Theorem: If a network has more than two odd vertices, it does not have an Euler path. Also, Theorem: If a network has two or less odd vertices, it has at least one Euler path.a vertex is odd if