Graphs Introduction and Depth first algorithm

Similar documents
LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Elements of Graph Theory

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

CS 310 Advanced Data Structures and Algorithms

CS 280 Problem Set 10 Solutions Due May 3, Part A

SELF-BALANCING SEARCH TREES. Chapter 11

ECE 242 HOMEWORK 5. In the figure below, one can go from node B to A but not from A to B.

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Chapter 9 Graph Algorithms

Binary Trees

Chapter 9 Graph Algorithms

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Graph Algorithms. 1 Preliminary Denitions. CSci 335 Software Design and Analysis III Chapter 9 Graph Algorithms. Prof.

Chapter 9 Graph Algorithms

Lecture 5: Graphs & their Representation

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CS 206 Introduction to Computer Science II

Undirected Graphs. Hwansoo Han

Data Structure. IBPS SO (IT- Officer) Exam 2017

Breadth First Search. cse2011 section 13.3 of textbook

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

GRAPHS Lecture 17 CS2110 Spring 2014

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Graphs & Digraphs Tuesday, November 06, 2007

Graph Search Methods. Graph Search Methods

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

March 20/2003 Jayakanth Srinivasan,

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)


GRAPHS Lecture 19 CS2110 Spring 2013

Understand graph terminology Implement graphs using

Basic Graph Definitions

CSE 417 Branch & Bound (pt 4) Branch & Bound

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Graphs - I CS 2110, Spring 2016

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

TIE Graph algorithms

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Discrete Mathematics 2 Exam File Spring 2012

Graph Algorithms Using Depth First Search

csci 210: Data Structures Graph Traversals

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

CMSC 451: Lecture 22 Approximation Algorithms: Vertex Cover and TSP Tuesday, Dec 5, 2017

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Data Structures Brett Bernstein

12 Abstract Data Types

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))


Introductory Remarks

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

) $ f ( n) " %( g( n)

Figure 1: A directed graph.

Lecture 10 Graph algorithms: testing graph properties

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

Traveling Salesperson Problem (TSP)

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions.

CMSC 380. Graph Terminology and Representation

Graphs Chapter 24. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Graphs. The ultimate data structure. graphs 1

Varying Applications (examples)

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

Math 15 - Spring Homework 5.2 Solutions

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

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

Graphs. What is a Graph? Computer Science S-111 Harvard University David G. Sullivan, Ph.D.

EECS 281 Homework 3 KEY Fall 2004

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 7

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH 1301 DISCRETE MATHEMATICS TIME ALLOWED: 2 HOURS

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

Basic Graph Algorithms (CLRS B.4-B.5, )

TIE Graph algorithms

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

Spanning Trees. Lecture 22 CS2110 Spring 2017

csci 210: Data Structures Graph Traversals

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

COT 6405 Introduction to Theory of Algorithms

Trees Algorhyme by Radia Perlman

Chapter 20: Binary Trees

Module 11: Additional Topics Graph Theory and Applications

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Goals! CSE 417: Algorithms and Computational Complexity!

CMSC th Lecture: Graph Theory: Trees.

3.1 Basic Definitions and Applications

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Overview. H. R. Alvarez A., Ph. D.

Announcements Problem Set 4 is out!

Graphs and Graph Algorithms. Slides by Larry Ruzzo

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

Transcription:

Graphs Introduction and Depth first algorithm Carol Zander Introduction to graphs Graphs are extremely common in computer science applications because graphs are common in the physical world. Everywhere you look, you see a graph. Intuitively, a graph is a set of locations and edges connecting them. A simple example would be cities on a map that are connected by roads. Or cities connected by airplane routes. Another example would be computers in a local network that are connected to each other directly. Constellations of stars (among many other applications) can be also represented this way. Relationships can be represented as graphs. Section 9. has many good graph examples. Graphs can be viewed in three ways (trees, too, since they are special kind of graph):. A mathematical construction this is how we will define them. An abstract data type this is how we will think about interfacing with them. A data structure this is how we will implement them The mathematical construction gives the definition of a graph: graph G = (V, E) consists of a set of vertices V (often called nodes) and a set of edges E (sometimes called arcs) that connect the edges. Each edge is a pair (u, v), such that u, v V. Every tree is a graph, but not vice versa. There are two types of graphs, directed and undirected. In a directed graph, the edges are ordered pairs, for example (u,v), indicating that a path exists from u to v (but not vice versa, unless there is another edge.) For the edge, (u,v), v is said to be adjacent to u, but not the other way, i.e., u is not adjacent to v. In an undirected graph, an edge (u,v) would be equivalent to (v,u). Labeled or weighted graphs have a number (weight) associated with each edge. The weight can represent a distance or cost or some meaningful value associated with the nodes. Here is an example of a weighted, directed graph. V V V 8 V V V A path between two nodes is a sequence of edges with which you can travel from one node to the other. The weight of the path is the sum of the weights of the edges. A common operation is to find the minimum weight (or distance) path between two nodes. For an unweighted graph, this would simply be the shortest path (that is, the fewest edges required). In a simple path, the nodes are unique except possibly for the first and last node. In the above graph, v v v v is a path, but not a simple path, whereas v v v is a simple path. The length of a path is the number of edges. Any path starting and ending at the same vertex (and including at least one edge) is called a cycle. In the above graph, v v v v is a cycle. An acyclic graph has no cycles. A connected graph has a path from every vertex to every other vertex. (These are usually undirected graphs, but not necessarily.) A completely connected graph has an edge between every pair of vertices. Page of

We have already seen one important problem that can be framed in terms of a graph: the traveling salesperson problem. Usually undirected graphs are used. Recall that the idea is to represent all of the cities that a traveling salesperson must visit as nodes in the graph, with edges between them representing the distance for the salesperson to travel between the cities. The problem is to determine a route starting in some city that travels to every other city once and returns to the start with the minimum travel cost. No algorithm has been found to solve this problem that is not exponential in the number of cities (nodes in the graph.) But, no one has proved that there isn t such a solution either. Graph representations We want to be able to represent a graph as a data structure in a computer. As with most of the data structures you have seen, there are two common representations, one uses just an array (often static allocation), and one uses linked lists (dynamic allocation, array of lists, or lists of lists). The simplest representation is an adjacency matrix which stores the adjacencies in a graph. If there are n vertices in the graph, then the adjacency matrix is an n x n array of values between the nodes. In an undirected graph, typically zero means there is no edge; a one means there is an edge. If the graph above had no labels, the adjacency matrix would be as follows. V V V V V V 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 If the graph has weights, then when an edge exists, the weight is stored. And when there is no edge, the value stored is infinity (or a very large number, in practice). The label for the edges is called weight, distance, cost, or whatever is appropriate. Here is the adjacency matrix for the first graph above that has weights. Page of

8 The other common representation is the adjacency list. In this case, there is a (one dimensional) array of linked lists that store, for each node, the set of nodes that are adjacent to it and the weights of the edges. This can also be implemented as a linked list of linked lists. Here is the array of lists version. / / 8 / / / / Which representation is better? The adjacency matrix allows direct access, but can take more space (and time) for sparse graphs. The adjacency list is easier to manage when vertices are added to or removed from the graph. We could also store each edge list in a binary search tree. However, it is common to traverse all of the edges starting at a vertex, so the binary search tree would help only with insert and remove operations (which are less common). Graph algorithms We will consider several graph algorithms. We start with graph traversals. In a linked list, there is only one sensible way to traverse the list. In a tree, there are several common ways to traverse the data structure: inorder, preorder, and postorder. In a graph, a depth first traversal and breadth first traversal are the common traversals. Page of

In a depth first you keep visiting adjacent nodes until you can t go any further; in other words, you go deeper and deeper into the graph and then back up. In a binary tree, a depth first traversal (often referred to as depth first search) is a preorder traversal. In a breadth first traversal, the breadth at a level is traversed before going deeper. There is no common breadth first traversal in a binary tree. It would mean you would examine all the children before you examined the children s children. Consider the following tree: 8 9 0 A preorder (or depth first traversal) is 8 9 0. A breadth first traversal is 8 9 0. Depth first search The depth first algorithm is a generalization of a preorder traversal, visiting adjacent nodes (children) then visiting their adjacent nodes, and so on. When we visit each vertex, we have to decide in what order to visit the adjacent vertex. We will assume that there is some underlying ordering implied by the node number (or index) and use this order to break ties. The graph would be stored as either an adjacency matrix or an adjacency list so there would be a natural node to choose. In my examples, the numbers represent this natural order. So we should always start at vertex (or 0 depending on where we start counting) and consider a smaller numbered node before a larger numbered node. void depthfirstsearch(...) { // mark all the vertices as not visited for v = to n { if (v is not visited) dfs(v); void dfs(v...){ // mark v as visited // output v (or do whatever with v), display gives depth-first order for each vertex w adjacent to v { if (w is not visited) { dfs(w); Page of

Here is an example (unconnected graph): 0 9 8 As you do a depth first traversal, redraw the graph, essentially showing an execution tree. When we start at one, this gives a depth first spanning tree. The whole thing is called a depth first spanning forest. 0 9 8 The depth first ordering is 8 9 0. In the depth first spanning forest, the edges representing the traversal are called tree edges or primary edges. Edges in the original graph drawing that now go down or forward are called forward edges. The edge (,) is an example of a forward edge. Edges now going up or back are called back edges. The edges (,) and (,) are two examples of back edges. Edges that cross from one branch to another are called cross edges. Three examples of cross edges are (,), (9,8), and (0,). If all the forward, back, and cross edges were added, it would be the original graph, just redrawn, giving it depth. Page of