CS302 - Data Structures using C++

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

Graph Traversals. CS200 - Graphs 1

Basic Graph Definitions

Module 11: Additional Topics Graph Theory and Applications

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

CS 206 Introduction to Computer Science II

Konigsberg Bridge Problem

Outline. Graphs. Divide and Conquer.

Chapter 28 Graphs and Applications. Objectives

Graphs & Digraphs Tuesday, November 06, 2007

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.

Inf 2B: Graphs, BFS, DFS

CSE 100: GRAPH ALGORITHMS

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

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

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

CS 491 CAP Introduction to Graphs and Search

Unweighted Graphs & Algorithms

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


Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Copyright 2000, Kevin Wayne 1

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

CS/COE

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Data Structures and Algorithms. Chapter 7. Graphs

Graphs. Data Structures 1 Graphs

csci 210: Data Structures Graph Traversals

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

GRAPHS Lecture 17 CS2110 Spring 2014

CS2 Algorithms and Data Structures Note 9

CS 310 Advanced Data Structures and Algorithms

Graph Search Methods. Graph Search Methods

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

Elementary Graph Algorithms CSE 6331

(Re)Introduction to Graphs and Some Algorithms

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

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

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

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

Graph Algorithms Using Depth First Search

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

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

國立清華大學電機工程學系. Outline

W4231: Analysis of Algorithms

Graphs - II CS 2110, Spring 2016

CS Algorithms and Complexity

Lecture 9 Graph Traversal

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

GRAPHS Lecture 19 CS2110 Spring 2013

CS302 - Data Structures using C++

Directed Graphs (II) Hwansoo Han

Introduction to Graphs. common/notes/ppt/

Algorithms: Lecture 10. Chalmers University of Technology

Undirected Graphs. Hwansoo Han

SELF-BALANCING SEARCH TREES. Chapter 11

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

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

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Total Score /1 /20 /41 /15 /23 Grader

Graph: representation and traversal

Graphs and Graph Algorithms. Slides by Larry Ruzzo

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

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

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

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

To list all vertices connected to a vertex u in graph G we can use the following loop:

CS4800: Algorithms & Data Jonathan Ullman

Algorithm Design (8) Graph Algorithms 1/2

Homework Assignment #3 Graph

CS Data Structures and Algorithm Analysis

CSI 604 Elementary Graph Algorithms

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

csci 210: Data Structures Graph Traversals

Graphs - II CS 2110, Spring 2016

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Fundamental Algorithms

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

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

22.1 Representations of graphs

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

1. Graph and Representation

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Minimum Spanning Trees Ch 23 Traversing graphs

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Graph Search. Adnan Aziz

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Chapter 9 Graph Algorithms

CS24 Week 8 Lecture 1

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

INF2220: algorithms and data structures Series 7

Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

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.

Agenda. Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Transcription:

CS302 - Data Structures using C++ Topic: Graphs - Introduction Kostas Alexis

Terminology In the context of our course, graphs represent relations among data items G = {V,E} A graph is a set of vertices (nodes) and A set of edges that connect the vertices A simplified case: a line graph

Terminology In the context of our course, graphs represent relations among data items G = {V,E} A graph is a set of vertices (nodes) and A set of edges that connect the vertices A graph is an ordered pair G = (V, E) comprising a set V of vertices, nodes or points together with a set E of edges, arcs or lines, which are 2-element subsets of V (i.e., an edge is associated with two vertices, and the association takes the form of the unordered pair of the vertices). To avoid ambiguity, this type of graph may be described precisely as undirected and simple.

Terminology A graph and one of its subgraphs

Terminology Examples of graphs that are either connected, disconnected, or complete

Terminology Undirected and directed graphs

Terminology Undirected and directed graphs

Terminology Undirected Graph: An undirected graph is a graph in which edges have no orientation. The edge (x, y) is identical to the edge (y, x). That is, they are not ordered pairs, but unordered pairs i.e., sets of two vertices {x, y} (or 2-multisets in the case of loops). The maximum number of edges in an undirected graph without a loop is n(n 1)/2.

Terminology Directed Graph: A directed graph or digraph is a graph in which edges have orientations. It is written as an ordered pair G = (V, A) (sometimes G = (V, E)) with V a set whose elements are called vertices, nodes, or points; A a set of ordered pairs of vertices, called arrows, directed edges (sometimes simply edges with the corresponding set named E instead of A), directed arcs, or directed lines. An arrow (x, y) is considered to be directed from x to y; y is called the head and x is called the tail of the arrow; y is said to be a direct successor of x and x is said to be a direct predecessor of y. If a path leads from x to y, then y is said to be a successor of x and reachable from x, and x is said to be a predecessor of y. The arrow (y, x) is called the inverted arrow of (x, y). A directed graph G is called symmetric if, for every arrow in G, the corresponding inverted arrow also belongs to G. A symmetric loopless directed graph G = (V, A) is equivalent to a simple undirected graph G = (V, E), where the pairs of inverse arrows in A correspond one-to-one with the edges in E; thus the number of edges in G is E = A /2, that is half the number of arrows in G.

Graphs as ADTs ADT graph operations

Graphs as ADTs ADT graph operations Test if empty Get number of vertices, edges in a graph See if edge exists between two given vertices Add vertex to graph whose vertices have distinct, different values from new vertex Add/remove an edge between two given vertices Remove vertex, edges to other vertices Retrieve vertex that contains a given value

Graphs as ADTs A C++ interface for undirected, connected graphs #ifndef GRAPH_INTERFACE_ #define GRAPH_INTERFACE_ template<class LabelType> class GraphInterface { public: /** Gets the number of vertices in this graph. @return The number of vertices in the graph. */ virtual int getnumvertices() const = 0; /** Gets the number of edges in this graph. @return The number of edges in the graph. */ virtual int getnumedges() const = 0;

Graphs as ADTs A C++ interface for undirected, connected graphs /** Creates an undirected edge in this graph between two vertices that have the given labels. If such vertices do not exist, creates them and adds them to the graph before creating the edge. @param start A label for the first vertex. @param end A label for the second vertex. @param edgeweight The integer weight of the edge. @return True if the edge is created, or false otherwise. */ virtual bool add(labeltype start, LabelType end, int edgeweight) = 0; /** Removes an edge from this graph. If a vertex is left with no other edges, it is removed from the graph since this is a connected graph. @param start A label for the vertex at the beginning of the edge. @param end A label for the vertex at the end of the edge. @return True if the edge is removed, or false otherwise. */ virtual bool remove(labeltype start, LabelType end) = 0; /** Gets the weight of an edge in this graph. @param start A label for the vertex at the beginning of the edge. @param end A label for the vertex at the end of the edge. @return The weight of the specified edge. If no such edge exists, returns a negative integer. */ virtual int getedgeweight(labeltype start, LabelType end) const = 0;

Graphs as ADTs A C++ interface for undirected, connected graphs /** Performs a depth-first search of this graph beginning at the given vertex and calls a given function once for each vertex visited. @param start A label for the beginning vertex. @param visit A client-defined function that performs an operation on or with each visited vertex. */ virtual void depthfirsttraversal(labeltype start, void visit(labeltype&)) = 0; /** Performs a breadth-first search of this graph beginning at the given vertex and calls a given function once for each vertex visited. @param start A label for the beginning vertex. @param visit A client-defined function that performs an operation on or with each visited vertex. */ virtual void breadthfirsttraversal(labeltype start, void visit(labeltype&)) = 0; /** Destroys this graph and frees its assigned memory. */ virtual ~GraphInterface() { } }; // end GraphInterface #endif

Implementing Graphs A directed graph and its adjacency matrix

Implementing Graphs A weighted undirected graph and its adjacency matrix

Implementing Graphs Weighted Graph: A weighted graph is a graph in which a number (the weight) is assigned to each edge. Such weights might represent for example costs, lengths or capacities, depending on the problem at hand.

Implementing Graphs A directed graph and its adjacency list

Implementing Graphs A weighted undirected graph and its adjacency list

Implementing Graphs Adjacency List Often requires less space than adjacency matrix Supports finding vertices adjacent to given vertex Adjacency Matrix Supports process of seeing if there exists from vertex i to vertex j

Graph Traversals Problem: Find if there is a path between two vertices of the graph Methods: Depth-First Search (DFS) or Breadth-First Search (BFS)

Graph Traversals Visits all vertices it can reach Visits all vertices if and only if the graph is connected Connected component Subset of vertices visited during a traversal that belongs at given vertex

Depth-First Search Depth-First Search (DFS) Goes as far as possible from a vertex before backing up

Depth-First Search Depth-First Search (DFS) Goes as far as possible from a vertex before backing up Recursive traversal algorithm // Traverses a graph beginning at vertex v by using a // depth-first search. Recursive version dfs(v: Vertex) { Mark v as visited for (each unvisited vertex u adjacent to v) dfs(u) }

Depth-First Search Results of a DFS traversal beginning at vertex a Node visited a b c d g e (backtrack) f (backtrack) (backtrack) h (backtrack) (backtrack) (backtrack) (backtrack) i (backtrack) (backtrack) Stack (top to bottom) a a b a b c a b c d a b c d g a b c d g e a b c d g a b c d g f a b c d g a b c d a b c d h a b c d a b c a b a a i a (empty)

Depth-First Search Results of a DFS traversal beginning at vertex a Node visited a b c d g e (backtrack) f (backtrack) (backtrack) h (backtrack) (backtrack) (backtrack) (backtrack) i (backtrack) (backtrack) Stack (top to bottom) a a b a b c a b c d a b c d g a b c d g e a b c d g a b c d g f a b c d g a b c d a b c d h a b c d a b c a b a a i a (empty)

Depth-First Search DFS uses a stack 3 2 1

Depth-First Search Applications: Finding connected components. Topological sorting. Finding 2-(edge or vertex)-connected components. Finding 3-(edge or vertex)-connected components. Finding the bridges of a graph. Generating words in order to plot the Limit Set of a Group. Finding strongly connected components. Planarity testing[7][8] Solving puzzles with only one solution, such as mazes. (DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the visited set.) Maze generation may use a randomized depth-first search. Finding biconnectivity in graphs.

Depth-First Search Maze generation

Breadth-First Search Breadth-First Search (BFS) traversal Visits all vertices adjacent to a vertex before going forward. BFS is a first visited, first explored strategy Contrast to DFS as last visited, first explored

Breadth-First Search Breadth-First Search (BFS) traversal Visits all vertices adjacent to a vertex before going forward. BFS is a first visited, first explored strategy Contrast to DFS as last visited, first explored Main Idea: Look at all possible paths at the same depth before you go at a deeper level Backup as far as possible when you reach a dead end i.e., next vertex has been marked or there is no next vertex startvertex endvertex

Breadth-First Search // Traverses a graph beginning at vertex v by using a // Breadth-First breadth-first search. Search Iterative (BFS) version traversal bfs(v: Vertex) { Visits all vertices adjacent to a vertex before going forward. BFS q = is a new a first empty visited, queue first explored strategy // Add v to queue and mark it Contrast to DFS as last visited, first explored q.enqueue(v) Mark v as visited while (!q.isempty()) { q.dequeue(w) // Loop invariant: there is a path from vertex w to every vertex in the queue q for (each unvisited vertex u adjacent to w) { Mark u as visited q.enqueue(u) } } }

Breadth-First Search Consider the graph

Breadth-First Search Results of a BFS traversal beginning at vertex a Node visited a blank b f i blank c e blank g blank blank d blank blank blank h blank Stack (top to bottom) a (empty) b b f b f i f i f i c f i c e i c e i c e g c e g e g e g d g d d (empty) h (empty)

Breadth-First Search Results of a BFS traversal beginning at vertex a Node visited a blank b f i blank c e blank g blank blank d blank blank blank h blank Stack (top to bottom) a (empty) b b f b f i f i f i c f i c e i c e i c e g c e g e g e g d g d d (empty) h (empty)

Breadth-First Search BFS uses a Queue 1 4 3 7 8 6 2 5

Applications of Graphs Topological Sorting Spanning Trees Minimum Spanning Trees Shortest Paths Circuits Crucial mathematical/engineering problems

Example Graph A connected graph with cycles

Topological Sorting The graph in the previous Figure arranged according to two topological orders

Topological Sorting topsort1 algorithm // Arranges the vertices in graph thegraph into a // topological order and places them in list alist. topsort1(thegraph: Graph, alist: List) { n = number of vertices in thegraph for (step = 1 through n) { Select a vertex v that has no successors alist.insert(1,v) Remove from thegraph vertex v and its edges } }

Topological Sorting A trace of topsort1 for the previous figure.

Topological Sorting (continued)

Topological Sorting A trace of topsort1 for the previous figure.

Topological Sorting (continued)

Topological Sorting topsort2 // Arranges the vertices in graph thegraph into a // topological order and places them in list alist. topsort2(thegraph: Graph, alist: List) { s = a new empty stack for (all vertices v in the graph) { if (v has no predecessors) { s.push(v) Mark v as visited } } while (!s.isempty())

Topological Sorting topsort2 while (!s.isempty()) { if (all vertices adjacent to the vertex on the top of the stack have been visited) { s.pop(v) alist.insert(1,v) } else { Select an unvisited vertex u adjacent to the vertex on the top of the stack s.push(u) Mark us as visited } } }

Thank you