CSC212 Data Structure - Section RS Lecture 23 Lecture 23 Introduction to Graphs Introduction to Graphs Instructor: Zhigang Zhu Instructor: Zhigang Zhu Department of Computer Department of Computer Science Science City College of New York City College of New York
18
Embed
CSC212 Data Structure - Section RS Lecture 23 Introduction to Graphs Instructor: Zhigang Zhu Department of Computer Science City College of New York.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSC212 Data Structure - Section RS
CSC212 Data Structure - Section RS
Lecture 23 Lecture 23
Introduction to GraphsIntroduction to Graphs
Instructor: Zhigang ZhuInstructor: Zhigang Zhu
Department of Computer Science Department of Computer Science
•Minimum distance between Dallas and Corpus Christi?
Examples: Delaunay Triangulation Network From a Point Data Set Examples: Delaunay Triangulation Network From a Point Data Set
Examples: Internet Topology 01/16/2006Examples: Internet Topology 01/16/2006http://www.eee.bham.ac.uk/com_test/img%5Cdsnl%5Cinternet15jan06.png
Examples: Backbone of science With 212 clusters comprising 7000 journals
Examples: Backbone of science With 212 clusters comprising 7000 journals
Biochemistry, Cell Biology, Biophyiscs
Mathematics
Computer Science
Geosciences, Geochemistry, Mineralogy
Kevin W Boyack, Richard Klavans, Katy Börner, Mapping the backbone of science, Scientometrics, Vol. 64, No. 3. (2005), pp. 351-374.
TerminologiesTerminologies
A graph G = (V, E)A graph G = (V, E) V: verticesV: vertices E : edges, pairs of vertices from V E : edges, pairs of vertices from V V V
Undirected Graph: Undirected Graph: (u,v) is same as (v,u)(u,v) is same as (v,u)
Directed GraphDirected Graph (u,v) is different from (v,u)(u,v) is different from (v,u) Source (u)Source (u) Target (v)Target (v)
A
E F G H
DCB
A
E F G H
DCB
I
More Terminologies – P 728More Terminologies – P 728
Loop: an edge that connects a vertex to itself. Loop: an edge that connects a vertex to itself. Path: a sequence of vertices, pPath: a sequence of vertices, p00,p,p11, …p, …pmm, such that , such that
each adjacent pair of vertices peach adjacent pair of vertices pii and p and pi+1i+1 are are connected by an edge.connected by an edge.
Multiple Edges: two or more edges connecting the Multiple Edges: two or more edges connecting the same two vertices in the same direction. same two vertices in the same direction.
Simple graph: have no loops and no multiple Simple graph: have no loops and no multiple edges – required for many applications. edges – required for many applications.
Weighted graph and unweighted graphWeighted graph and unweighted graph
An adjacency matrix represents the graph as a n x n matrix A:A[i, j] = 1 if edge (i, j) E (or weight of edge)
= 0 if edge (i, j) E
01
23
0 1 2 3
0 11 00 11 00
1 11 00 00 00
2 00 00 00 11
3 11 00 11 00
Space Complexity with respect to |V| and/or |E|?
Representations-Linked ListRepresentations-Linked List
01
23
A directed graph with n vertices can be represented by n different linked lists. List number i provides the connections for vertex i. To be specific: for each entry j in the list number i, there is an edge from i to j.
00
11
22
33
22
00
00
00
33
22Space Complexity with respect to |V| and/or |E|?
Graph TraversalsGraph Traversals
Traversal: Traversal: Tree traversals (ch 10): visit all of a tree’s nodes and do Tree traversals (ch 10): visit all of a tree’s nodes and do
some processing at each nodesome processing at each node Types of Graph traversalsTypes of Graph traversals
Depth First Search (DFS)Depth First Search (DFS) Breadth First Search (BFS)Breadth First Search (BFS)
Issues to considerIssues to consider There is no root – need a start vertexThere is no root – need a start vertex Be careful and do not enter a repetitive cycle – mark Be careful and do not enter a repetitive cycle – mark
each vertex as it is processedeach vertex as it is processed
Initialize the boolean Initialize the boolean visitedvisited array array Rec_DFS(Rec_DFS(startstart))
Rec_DFS(Rec_DFS(startstart)) For each of unvisited neighbor For each of unvisited neighbor nextnext of of startstart
Rec_DFS(Rec_DFS(nextnext))
0
1
6
2
4
3 5
00
1
6
2
4
3 5
0,10
1
6
2
4
3 5
0,1,3
Graph Traversal-DFSGraph Traversal-DFS
0
1
6
2
4
3 5
0,1,3,50
1
6
2
4
3 5
0,1,3,5,6
0
1
6
2
4
3 5
0,1,3,5,6,4
0
1
6
4
3 5
•2 is never visited
•DFS search/spanning tree
•Non-recursive version? (using a stack)
Graph Traversal-BFSGraph Traversal-BFS
BFS(BFS(startstart)) Initialize the boolean visited arrayInitialize the boolean visited array Add Add startstart to an empty queue Q to an empty queue Q Visited[Visited[startstart]=true;]=true; While(!Q.empty())While(!Q.empty())
u=Q.dequeue () //top+pop/get_frontu=Q.dequeue () //top+pop/get_front For each unvisited neighbor v of u For each unvisited neighbor v of u
Path Algorithms (Ch 15.4)Path Algorithms (Ch 15.4) Dijkstra single source pathDijkstra single source path CSc 220 AlgorithmCSc 220 Algorithm
Alternative Bellman-Ford algorithm for graphs with Alternative Bellman-Ford algorithm for graphs with negative weights negative weights http://en.wikipedia.org/wiki/Bellman-Ford_algorithmhttp://en.wikipedia.org/wiki/Bellman-Ford_algorithm