Graph.

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graph & BFS.
Connected Components, Directed Graphs, Topological Sort COMP171.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
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.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs A New Data Structure
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Graphs Breadth First Search & Depth First Search
CS 201: Design and Analysis of Algorithms
Graphs Chapter 20.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Data Structures, Algorithms & Complexity
Lecture 11 Graph Algorithms
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Introduction to Graphs
Depth-First Search.
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
CS120 Graphs.
Graph Algorithm.
Binhai Zhu Computer Science Department, Montana State University
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph & BFS.
Finding Shortest Paths
Graphs Representation, BFS, DFS
BFS,DFS Topological Sort
Graph Algorithm.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
BFS,DFS Topological Sort
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
CSE 373 Data Structures Lecture 16
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.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Elementary Graph Algorithms
Lecture 10 Graph Algorithms
Graph Algorithm.
Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
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:

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 Problems 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) Examples 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 A graph G = (V, E) Most graph problems are undirected w w x x y y z 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 x z y 5 3 1 7

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] = 1, if (i, j)  E 0, if (i, j)  E good easy to check if an edge (i, j) is in E easy to add/remove edges bad space overhead if n >> m

Examples the City (Manhattan) – not so big area 1 2 1 2 3 3 5 4 5 4 1 2 3 4 5 1 2 3 4 5 1 1 1 1 2 2 3 3 4 4 5 5 the City (Manhattan) – not so big area 15 avenues and 200 streets 3000 vertices and 6000 edges 3000 X 3000 = 9,000,000 cells VLSI chip with 15 million transistors

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

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

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

Terminologies 2 - incident directed (x  y) 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 2: (1, 2), (2, 5)

Terminologies 3 Degree of a vertex 1 2 3 4 5 6 1 2 3 4 5 6 undirected the number of edges incident on it. ex. vertex 2 in the graph has degree 2. 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 2 in the right graph in-degree = 2 out-degree = 3 degree = 2+3 = 5 1 2 3 4 5 6 1 2 3 4 5 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, 1, 2, false) 3 1 2 … 50 … 1 1 2 2 … … 100 100 edges[101][51] degree[101]

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

BFS 2 4 8 s 5 7 3 6 9

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

1 1 Undiscovered Queue: s 2 3 Discovered Top of queue Finished 2 4 8 s s 5 7 3 3 6 9 1 Undiscovered Queue: s 2 3 Discovered Top of queue Finished

1 1 1 Undiscovered Queue: s 2 3 5 Discovered Top of queue Finished 2 4 8 s 5 5 7 1 3 6 9 1 Undiscovered Queue: s 2 3 5 Discovered Top of queue Finished

1 2 1 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished 2 8 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished

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

1 2 1 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished 2 8 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished

1 2 1 1 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished 2 4 8 s 5 7 1 3 6 9 1 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished

1 2 1 1 2 Undiscovered Queue: 3 5 4 6 Discovered Top of queue Finished 8 s 5 7 1 3 6 6 9 1 2 Undiscovered Queue: 3 5 4 6 Discovered Top of queue Finished

1 2 1 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished 2 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished

1 2 1 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished 2 4 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished

1 2 3 1 1 2 Undiscovered Queue: 4 6 8 Discovered Top of queue Finished s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 8 Discovered Top of queue Finished

1 2 3 1 3 1 2 Undiscovered Queue: 6 8 7 Discovered Top of queue 4 8 s 5 7 7 1 3 3 6 9 1 2 Undiscovered Queue: 6 8 7 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 6 8 7 9 Discovered Top of queue 4 8 s 5 7 1 3 3 6 9 9 3 1 2 Undiscovered Queue: 6 8 7 9 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 8 7 9 Discovered Top of queue 4 8 s 5 7 1 3 3 6 9 9 3 1 2 Undiscovered Queue: 8 7 9 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished

1 2 3 1 3 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished

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

BFS Algorithm Initialization 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] + 1 [v]  u ENQUEUE(Q, v) color[u]  GREEN Initialization

DFS similar to Backtracking stack is an ideal candidate 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 do if edge e is unexplored then w  opposite(v, e) // return the end point of e distant to v if vertex w is unexplored then mark e as a discovered edge recursively call DFS(G, w) else mark e as a back edge

A B H C G I D E F 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: B H C G I D E F

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

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 1 b c a c a 2 3 d d 4

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

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

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

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

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

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

span e; Queue is empty in_deg=0 in_deg=0 in_deg=0 in_deg=0 Queue f in_deg=0 in_deg=0 c e Queue b d e in_deg=0 in_deg=0 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 1 Is a given undirected graph bicolorable?

Problem 2 input: 4 digits number; S1 S2 S3 S4 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