Download presentation

Presentation is loading. Please wait.

Published byClement Hutchinson Modified about 1 year ago

1
Graphs

2
Data structures that connect a set of objects to form a kind of a network Objects are called “Nodes” or “Vertices” Connections are called “Edges” 2

3
Types of Graphs 3 Directed vs. undirected Undirected (Edges have no direction) Directed (Edges have directions) Weighted vs. unweighted Unweighted (Edges have no cost/weight) Weighted (Edges have associated cost/weight)

4
Types of Graphs (Cont’d) 4 Dense vs. Sparse Dense graphs (many edges between nodes) Sparse graphs (few edges between nodes) If the graph has n vertices (nodes) Maximum # of edges is n 2 In dense graphs number of edges is close to n 2 In sparse graphs number of edges is close to n

5
5 Some Graph Applications transportation Graph street intersections NodesEdges highways communicationcomputersfiber optic cables World Wide Webweb pageshyperlinks socialpeoplerelationships food webspeciespredator-prey software systemsfunctionsfunction calls schedulingtasksprecedence constraints circuitsgateswires

6
6 World Wide Web Web graph. n Node: web page. n Edge: hyperlink from one page to another. cnn.com cnnsi.com novell.comnetscape.com timewarner.com hbo.com sorpranos.com

7
7 Social Network Social network graph. n Node: people. n Edge: relationship between two people. Reference: Valdis Krebs,

8
Protein Networks 8 Nodes are proteins Edges are connections (interaction between proteins)

9
More Formalization 9

10
10 Undirected Graphs Undirected graph. G = (V, E) n V = set of nodes or vertices n E = edges between pairs of nodes. n Graph size parameters: n = |V|, m = |E|. 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 = |V| = 8 m = |E| = 11

11
Graph Representation Two main methods 11 Adjacency MatrixAdjacency List

12
12 Graph Representation: Adjacency Matrix Adjacency matrix. V-by-V matrix (A) n A[i, j] = 1 if exists edge between node i and node j n Space proportional to V 2 n Checking if (u, v) is an edge takes (1) time. n Identifying all edges takes (V 2 ) time. n For undirected graph matrix is symmetric across the diagonal Vertices

13
13 Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. n Two representations of each edge. n Space proportional to O(E + V). n Checking if (u, v) is an edge takes O(deg(u)) time. n Identifying all edges takes (E + V) time degree = number of neighbors of u 37

14
Degree of a Node In-degree(v): Number of edges coming to (entering) node v Out-degree(v): Number of edges getting out (leaving) node v For Undirected graphs In-degree = Out-degree 14 In-degree(V3) = Out-degree(3) = 5 In-degree(E) = 2 Out-degree(E) = 3

15
Graph Traversal 15

16
Graph Traversal Graph Traversal means visiting each node in the graph There is a starting node (s) Two main types of traversal Breadth-First-Search (BFS) Depth-First-Search (DFS) Both are applicable for directed and undirected graphs 16 BFSDFS

17
17 Breadth First Search s s L1L1 L2L2 L n-1 Visit the nodes one-level at a time Requires a queue (First-come-first-served)

18
18 Breadth First Search s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s

19
19 Breadth First Search s Undiscovered Discovered Finished Queue: s 2 Top of queue 3 1 1

20
20 Breadth First Search s Undiscovered Discovered Finished Queue: s 2 3 Top of queue

21
21 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue 1 1 1

22
22 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

23
23 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue already discovered: don't enqueue

24
24 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

25
25 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

26
26 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

27
27 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

28
28 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

29
29 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

30
30 Breadth First Search s Undiscovered Discovered Finished Queue: 4 6 Top of queue

31
31 Breadth First Search s Undiscovered Discovered Finished Queue: 4 6 Top of queue

32
32 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

33
33 Breadth First Search s Undiscovered Discovered Finished Queue: 6 8 Top of queue

34
34 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

35
35 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

36
36 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

37
37 Breadth First Search s Undiscovered Discovered Finished Queue: 7 9 Top of queue

38
38 Breadth First Search s Undiscovered Discovered Finished Queue: 7 9 Top of queue

39
39 Breadth First Search s Undiscovered Discovered Finished Queue: 7 9 Top of queue

40
40 Breadth First Search s Undiscovered Discovered Finished Queue: 7 9 Top of queue

41
41 Breadth First Search s Undiscovered Discovered Finished Queue: 9 Top of queue

42
42 Breadth First Search s Undiscovered Discovered Finished Queue: 9 Top of queue

43
43 Breadth First Search s Undiscovered Discovered Finished Queue: 9 Top of queue

44
44 Breadth First Search s Undiscovered Discovered Finished Queue: Top of queue

45
45 Breadth First Search s Starting from s, we visited all (reachable) nodes BFS forms a tree rooted at s (BFS Tree) For each node x reachable from s we created a shortest path from s to x

46
46 Breadth First Search Example problems in which we use BFS Find if node x is reachable from node y Start from node y and do BFS Find the shortest path from node x to node y Start from node x and perform BFS Search for a value v in the graph Start from any node and perform BFS Always keep in mind whether we talk about undirected graph or directed graph

47
47 Breadth First Search: Algorithm Start at node v Can do any processing on t

48
48 Breadth First Search: Analysis Theorem. The above implementation of BFS runs in O(V + E) time if the graph is given by its adjacency list. Proof n Each node will be queued only once O(V) n For each node, we visit all its out edges O(E) – In fact each edge (u,v) is visited twice once from u’s side and once form v’s side n Total is O(V + E)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google