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

Similar documents
Lecture 9 Graph Traversal

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Basic Graph Algorithms

Elementary Graph Algorithms

Graph Representation

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

Graph Search. Adnan Aziz

Lecture 9 Graph Traversal

CSI 604 Elementary Graph Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Chapter 22. Elementary Graph Algorithms

Lecture 9 Graph Traversal

Design and Analysis of Algorithms

Graph representation

Graph Algorithms. Definition

COT 6405 Introduction to Theory of Algorithms

Representations of Graphs

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graph: representation and traversal

Outlines: Graphs Part-2

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Announcements Problem Set 4 is out!

Graph implementations :

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

Minimum Spanning Trees Ch 23 Traversing graphs

Elementary Graph Algorithms

Review: Graph Theory and Representation

Unit 2: Algorithmic Graph Theory

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

CSC263 Week 8. Larry Zhang.

Algorithm Design and Analysis

csci 210: Data Structures Graph Traversals

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

CS 310 Advanced Data Structures and Algorithms

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Homework Assignment #3 Graph

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

W4231: Analysis of Algorithms

Figure 1: A directed graph.

Graphs & Digraphs Tuesday, November 06, 2007

22.3 Depth First Search

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

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

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Outline. Graphs. Divide and Conquer.

Chapter 22 Elementary Graph Algorithms

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

DFS & STRONGLY CONNECTED COMPONENTS

Announcements. HW3 is graded. Average is 81%

Introduction to Algorithms. Lecture 11

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


Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Basic Graph Definitions

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Inf 2B: Graphs, BFS, DFS

Topic 12: Elementary Graph Algorithms

Graph: representation and traversal

Suggested Study Strategy

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.

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

Algorithm Design and Analysis

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

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

811312A Data Structures and Algorithms, , Exercise 6, solution

Copyright 2000, Kevin Wayne 1

Sample Solutions to Homework #4

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Final Exam. EECS 2011 Prof. J. Elder - 1 -

COP 4531 Complexity & Analysis of Data Structures & Algorithms

csci 210: Data Structures Graph Traversals

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Chapter 9 Graph Algorithms

Breadth First search and Depth First search Finding connected components

CSE 373: Data Structures and Algorithms

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

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

CS/COE

Algorithm Design and Analysis

Lecture 26: Graphs: Traversal (Part 1)

Transcription:

Graph

Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance or cost) path

Graph Theory Many problems are mapped to graphs traffic VLSI circuits social network communication networks web pages relationship Problems how can a problem be represented as a graph? how to solve a graph problem?

Graph Notations A graph G = (V, E) V is a set of vertices(nodes) E is a set of edges E = (x, y) where x, y V ordered or unordered pairs of vertices from V Examples map landmarks or cities are vertices roads are edges program analysis a line of program statement is a vertice the next line to be executed is connected thru edge

Graphs w x w x y z y z A graph G = (V, E) is undirected if edge (x, y) E implies that (y, x) E, too. is directed if not Most graph problems are undirected

Graphs weighted or unweighted w 5 x w x 7 y z y z

Graphs acyclic a graph without any cycle undirected (free tree) directed (DAG) the most important one

Graph Representation G = (V, E), V =n and E =m adjacency-matrix n x n matrix M M[i, j] =, if (i, j) E 0, if (i, j) E good bad easy to check if an edge (i, j) is in E easy to add/remove edges space overhead if n >> m

Examples 5 4 5 4 4 5 0 0 0 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 4 0 0 0 0 0 5 0 0 5 0 0 0 0 the City (Manhattan) not so big area 5 avenues and 00 streets 000 vertices and 6000 edges 000 X 000 = 9,000,000 cells VLSI chip with 5 million transistors

Graph Representation 5 4 5 4 5 / 5 4 / 4 / 4 5 / 5 4 / 5 5 / 5 / 4 / 4 / space efficient for sparse graphs problems?

Graph Representation mixed version use array instead of linked lists looks good? alternatives? 5 4 5 5 4 4 5 5 4 4 4 5 4 5 5 4 5 4

Terminologies - adjacency clear for undirected grapg for directed graph is adjacent to is NOT adjacent to make a formal definition if y is adjacent to x, we write x y 4 5 6

Terminologies - incident directed an edge (x, y) is incident from (or leaves) vertex x and is incident to (or enters) vertex y. undirected an edge (x, y) is incident on vertices x and y ex. the edges incident on vertex : (, ), (, 5)

Terminologies Degree of a vertex undirected the number of edges incident on it. ex. vertex in the graph has degree. A vertex whose degree is 0, i.e., vertex 4 in the graph, is isolated. directed out-degree of a vertex : the number of edges leaving it in-degree of a vertex : the number of edges entering it degree of a vertex : its in-degree + out-degree ex. vertex in the right graph in-degree = out-degree = degree = + = 5 4 5 4 5 6 6

Adjacency matrix structure only if you know MAXDEGREE otherwise, MAXV X MAXV symmetric for undirected graph waste of space

adding an edge - insert_edge(g,,, false) 50 00 50 0 0 0 edges[0][5] degree[5]

Graph Traversal visit vertices and edges all of them for completeness exactly once for efficiency Breadth First Search (BFS) Depth First Search (DFS)

BFS 4 8 s 5 7 6 9

Shortest path from s 4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: s

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: s

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: s 5

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 5 4

4 8 0 s 5 5 already discovered: 7 don't enqueue 6 9 Undiscovered Discovered Top of queue Finished Queue: 5 4

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 5 4

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 5 4

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 5 4 6

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 5 4 6

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 4 6

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 4 6 8

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 6 8 7

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 6 8 7 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 8 7 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 7 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 7 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 7 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: 9

4 8 0 s 5 7 6 9 Undiscovered Discovered Top of queue Finished Queue: Since Queue is empty, STOP!

BFS Algorithm BFS(G, s) for each vertex u V {s} do color[u] GRAY d[u] [u] NIL color[s] BLUE d[s] 0 [s] NIL ENQUEUE(Q, s) while (Q ) do u DEQUEUE(Q) for each v Adj[u] do if color[v] GRAY then color[v] BLUE d[v] d[v] + [v] u ENQUEUE(Q, v) color[u] GREEN Initialization

DFS similar to Backtracking go as deep as you can next one is your siblings stack is an ideal candidate DFS(G, v) for all edges e incident on v distant to v do if edge e is unexplored then w opposite(v, e) // return the end point of e if vertex w is unexplored then else mark e as a discovered edge recursively call DFS(G, w) mark e as a back edge

F A B H C G I D E Adjacency Lists A: F G B: A I C: A D D: C F E: C D G F: E: G: : H: B: I: H:

assume left child first A Function call stack: B H C G I D F E

Topological Sort Definition A topological sort of a DAG G is a linear ordering of all its vertices such that if G contains a link (u,v), then node u appears before node v in the ordering b b c a c a d 4 d

Algorithm Example find source nodes (indegree = 0) if there is no such node, the graph is NOT DAG in_deg= a in_deg= f c in_deg= e b in_deg= d in_deg= c Queue Sorted: -

span c; decrement in_deg of a, b, e store a in Queue since in_deg becomes 0 a in_deg= f c in_deg= e b in_deg= d in_deg= a c Queue Sorted: c

span a; decrement in_deg of b, f store b, f in Queue since... a f c in_deg= e b d in_deg= f b a Queue Sorted: c a

span b; store d in Queue a f c in_deg= e b d d f b Queue Sorted: c a b

span f; decrement in_deg of e no node with in_deg = 0 is found a f c in_deg= e b d d f Queue Sorted: c a b f

span d; store e in Queue. a f c e b d e d Queue Sorted: c a b f d

span e; Queue is empty a f c e b d e Queue Sorted: c a b f d e

Example Algorithm Summary Based on indegree of each vertex if it is 0, this node is the first one in the sorted list span this node move this node from Queue to the sorted list find nodes edged from this node decrement indegrees of them It is so similar to BFS can you do it like DFS?

입력차수 0 인노드에서시작! 큐가비워질때까지루프내의동작을수행! 노드 y 와연결된노드의입력차수를하나씩감소! 입력차수가 0 인노드가생성되면큐에저장!

Problems Is a given undirected graph bicolorable?

Problem input: 4 digits number; S S S S 4 each digit can increment/decrement by one find minimal number of dec/inc operations to reach a target four digits number there are n forbidden digits where you should not reach at during operations