graphs adt

Upload: kirsty-sant

Post on 05-Jul-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/15/2019 Graphs ADT

    1/65

    Data Structures and Algorithms

    Graphs

    Prepared by Kristian Guillaumier

    Department of Intelligent Computer Systems

  • 8/15/2019 Graphs ADT

    2/65

    Note on the slides

    • Any code or pseudo-code shown in these

    slides may not be the most efficient or optimal

    way to implement an algorithm. The purpose

    of code in these slides is to support an

    argument or an explanation in class.

    • Send corrections to

    [email protected]

    2

  • 8/15/2019 Graphs ADT

    3/65

    Revision: Trees

    • Simple binary tree from Wikipedia:

    3

  • 8/15/2019 Graphs ADT

    4/65

    Tree Data Structures

    • Operations:

     – Initialising the tree.

     – Insert children.

     – Traversing the tree.

     – Traversing a sub tree.

     – Deleting an item.

     – Searching for an item.

    4

  • 8/15/2019 Graphs ADT

    5/65

    Binary Tree

    class Node

    {

    public int Value = 0;

    public Node LeftChild = null;

    public Node RightChild = null;

    }

    5

  • 8/15/2019 Graphs ADT

    6/65

    Binary Tree

    static Node CreateNode(int value)

    {

    Node newNode = new Node();

    newNode.Value = value;

    newNode.LeftChild = null;

    newNode.RightChild = null;

    //

    return newNode;

    }

    6

  • 8/15/2019 Graphs ADT

    7/65

    Binary Tree

    static void Main(string[] args)

    {

    Node root = CreateNode(10);

    root.LeftChild = CreateNode(3);

    root.RightChild = CreateNode(44);

    root.LeftChild.LeftChild = CreateNode(11);

    root.LeftChild.RightChild = CreateNode(8);

    root.RightChild.LeftChild = CreateNode(99);

    Console.ReadLine();

    }

    7

  • 8/15/2019 Graphs ADT

    8/65

    Binary Tree – Preorder Traversal

    static void PrintTree(Node node, int depth)

    {

    if (node = null)

    {

    Console.Write(new string(' ', depth));

    Console.WriteLine(node.Value);

    PrintTree(node.LeftChild, depth + 1);

    PrintTree(node.RightChild, depth + 1);

    }

    else

    {

    Console.Write(new string(' ', depth));

    Console.WriteLine("-");

    }

    }

    8

  • 8/15/2019 Graphs ADT

    9/65

    Binary Tree – Preorder Traversal

    9

  • 8/15/2019 Graphs ADT

    10/65

    Binary Tree – Post Order Traversal

    static void PrintTreePost(Node node, int depth)

    {

    if (node = null)

    {

    PrintTreePost(node.LeftChild, depth + 1);

    PrintTreePost(node.RightChild, depth + 1);

    Console.Write(new string(' ', depth));

    Console.WriteLine(node.Value);

    }

    else

    {

    Console.Write(new string(' ', depth));

    Console.WriteLine("?");

    }

    }

    10

  • 8/15/2019 Graphs ADT

    11/65

    Binary Tree – Post Order Traversal

    11

  • 8/15/2019 Graphs ADT

    12/65

    Binary Tree – Postorder Traversal

    • Useful to convert an expression in prefix

    notation (Polish notation).

    • Example from Wikipedia (2+3)*4:

    12

  • 8/15/2019 Graphs ADT

    13/65

    Binary Tree – In Order Traversal

    static void PrintTreeIn(Node node, int depth)

    {

    if (node = null)

    {

    PrintTreeIn(node.LeftChild, depth + 1);

    Console.Write(new string(' ', depth));

    Console.WriteLine(node.Value);

    PrintTreeIn(node.RightChild, depth + 1);

    }

    else

    {

    Console.Write(new string(' ', depth));

    Console.WriteLine("?");

    }

    }

    13

  • 8/15/2019 Graphs ADT

    14/65

    Binary Tree – In Order Traversal

    14

  • 8/15/2019 Graphs ADT

    15/65

    Binary Tree – In Order Traversal

    • If we do an in order traversal of a binary

    search tree, the sequence of items returned

    will be sorted.

    15

  • 8/15/2019 Graphs ADT

    16/65

  • 8/15/2019 Graphs ADT

    17/65

    Binary Tree – Breadth First

    17

  • 8/15/2019 Graphs ADT

    18/65

    Binary Search Tree

    • In a BST all the nodes in the left sub tree have a value less than the value

    of the current node.

    • All the nodes in the right sub tree have a value greater than or equal to

    the value of the current node.

    From Wikipedia:

    18

  • 8/15/2019 Graphs ADT

    19/65

    Binary Search Tree

    static void InsertValue(Node node, Node newNode)

    {

    if (newNode.Value < node.Value)

    {

    if (node.LeftChild == null)

    {

    node.LeftChild = newNode;

    }

    else

    {

    InsertValue(node.LeftChild, newNode);

    }

    }

    else

    {

    if (node.RightChild == null)

    {

    node.RightChild = newNode;

    }

    else

    {

    InsertValue(node.RightChild, newNode);

    }

    }

    }

    19

  • 8/15/2019 Graphs ADT

    20/65

    Binary Search Tree

    20

  • 8/15/2019 Graphs ADT

    21/65

    Binary Search Tree

    static bool BstFind(Node node, int value)

    {

    if (node == null)

    {

    return false;

    }

    else

    {

    if (value == node.Value)

    return true;

    else if (value < node.Value)

    return BstFind(node.LeftChild, value);

    else

    return BstFind(node.RightChild, value);

    }

    }

    21

  • 8/15/2019 Graphs ADT

    22/65

    Degenerate Trees and Analysis

    • Degenerate tree: for everyparent, only one child isassociated.

    • In worst case, tree isdegenerate – linear search – O(n).

    In average case, tree isperfectly balanced – O(log2n).

    22

  • 8/15/2019 Graphs ADT

    23/65

    Self Balancing Binary Trees

    • Attempt to keep the depth of the tree at a

    minimum (avoid degeneration).

    • Algorithms use tree rotations. From

    Wikipedia:

    23

  • 8/15/2019 Graphs ADT

    24/65

    LL Rotation (From Wikipedia)

    24

  • 8/15/2019 Graphs ADT

    25/65

    LLRotate

    static void LLRotate(Node node)

    {

    Node temp = node.RightChild;

    node.RightChild = node.LeftChild;

    node.LeftChild = node.RightChild.LeftChild;

    node.RightChild.LeftChild = node.RightChild.RightChild;

    node.RightChild.RightChild = temp;

    //

    int tempVal = node.Value;

    node.Value = node.RightChild.Value;

    node.RightChild.Value = tempVal;

    }

    25

  • 8/15/2019 Graphs ADT

    26/65

    RR Rotation

    26

  • 8/15/2019 Graphs ADT

    27/65

    RRRotate

    static void RRRotate(Node node)

    {

    Node temp = node.LeftChild;

    node.LeftChild = node.RightChild;

    node.RightChild = node.LeftChild.RightChild;

    node.LeftChild.RightChild = node.LeftChild.LeftChild;

    node.LeftChild.LeftChild = temp;

    //

    int tempVal = node.Value;

    node.Value = node.LeftChild.Value;

    node.LeftChild.Value = tempVal;

    }

    27

  • 8/15/2019 Graphs ADT

    28/65

    LR Rotation

    28

  • 8/15/2019 Graphs ADT

    29/65

    RL Rotation

    29

  • 8/15/2019 Graphs ADT

    30/65

    LRRotate/RLRotate

    static void LRRotate(Node node)

    {

    RRRotate(node.LeftChild);

    LLRotate(node);

    }

    static void RLRotate(Node node)

    {

    LLRotate(node.RightChild);

    RRRotate(node);

    }

    30

  • 8/15/2019 Graphs ADT

    31/65

    Another ‘Nice Site’ 

    • http://www.cs.bham.ac.uk/~mhe/foundations

    2/node1.html 

    31

    http://www.cs.bham.ac.uk/~mhe/foundations2/node1.htmlhttp://www.cs.bham.ac.uk/~mhe/foundations2/node1.htmlhttp://www.cs.bham.ac.uk/~mhe/foundations2/node1.htmlhttp://www.cs.bham.ac.uk/~mhe/foundations2/node1.html

  • 8/15/2019 Graphs ADT

    32/65

    Graphs

    • Some graph notes and examples are taken from:

     – http://www.brpreiss.com/books/opus6/html/page522.html

    ADT: – Collection of nodes/vertices.

     – Nodes are connected by edges/arcs.

     – G = (V, E).

     – E = (V1, V2). – Example Operations:

    • Finding a path between nodes.

    32

  • 8/15/2019 Graphs ADT

    33/65

    Basics

    • Ordered pair: – Collection of 2 objects a and b.

     – Written as (a, b).

     – (a, b) is different from (b, a).

     – a is distinguished as the first item.

     – b is distinguished as the second.

     – a can be = b.

    • Set: – A sequence of elements:

     – Order and repetitions are irrelevant.

     – {a, b} = {b, a} = {b, a, b}.

    33

  • 8/15/2019 Graphs ADT

    34/65

    Directed Graphs

    • Ordered pair G = (V, E)

    • V: finite, non-empty set of vertices.

    • E: finite, non-empty set of ordered pairs called

    edges.

     – E  V × V

    • Note that an edge – an ordered pair of

    vertices – (a, c) is distinct from (c, a).

    34

  • 8/15/2019 Graphs ADT

    35/65

    Example

    • G = (V1, E1)

    • Where:

     – V1 = {a, b, c, d}

     – E1 = {(a,b), (a,c), (b,c) …} 

    • Giving:

    35

  • 8/15/2019 Graphs ADT

    36/65

    Basics

    • Sometimes an edge (v, w)  E is written v  w. – The arrow is called a directed arc.

     – w is the head of the arc.

     – v is the tail of the arc.

    • Vertex w is adjacent to vertex v.• An edge v  w is said to emanate from v.

    • The notation A(v) is used to denote the set of edgesemanating from node v.

    • An edge v  w is said to be incident to w.• The notation I(w) is used to denote the set of edges

    incident to node w.

    36

  • 8/15/2019 Graphs ADT

    37/65

    Basics

    • Order of a graph: – |V| = number of vertices.

    • Size of a graph: – |E| = number of edges.

    • Out-Degree of a vertex: – The number of edges emanating from a node.

     – Written as |A(v)|

    In-Degree of a vertex: – The number of edges incident on a node.

     – Written as |I(v)|

    37

  • 8/15/2019 Graphs ADT

    38/65

    Basics

    38

  • 8/15/2019 Graphs ADT

    39/65

    Basics

    • Inverted edges:

     – If (u, v) is an edge e1 then e2 = (v, u) is e1

    inverted.

    • Pseudograph/Multigraph:

     – Two vertices may be connected by more than one

    edge e.g. vertices a and b and edges ((b,a), (b,a)).

    39

  • 8/15/2019 Graphs ADT

    40/65

    Walks

    • A walk in a graph G is a non-empty sequence ofvertices: – P = {v1, v2, v3, …, vk}

     – The length of a walk is the number of edges in the

    walk. – Open walk: start and end vertices are not the same.

     – Closed walk: start and end vertices are the same.

     – The length of an open walk is (n-1) where n is the

    number of vertices visited. – The length of a closed walk is (n) where n is the

    number of vertices visited.

    40

  • 8/15/2019 Graphs ADT

    41/65

    Basics

    • Each vertex vi in a walk/path has a successor vi+1 exceptthe last.

    • Each vertex vi in a walk/path has a predecessor vi-1 except the first.

    • A walk is simple iff vi != v j such that: – 1

  • 8/15/2019 Graphs ADT

    42/65

    Paths

    • A path is a simple walk (no vertex is repeated).

    • May be open or closed.

    42

  • 8/15/2019 Graphs ADT

    43/65

    Definitions

    • Cycle:

     – Closed path – no vertex is repeated except start and

    end vertices. v1 = vk.

     –The length of a cycle is the length of the path.

    • Loops:

     – A loop is a cycle whose length = 1 i.e. {v,v}

    Acyclic: – A graph without cycles.

     – A tree is a connected, directed, simple, acyclic graph.

    43

  • 8/15/2019 Graphs ADT

    44/65

    Basics

    • Subgraph:

     – The subgraph S of a graph G is one where:

    • Each vertex in S is a vertex in V of G.

    • Each edge in S: – Is a subset of the edges in G.

     – Each vertex in the edges of S is a vertex in S.

    • Connected graph:

     – Every pair of vertices is connected by an edge.

    44

  • 8/15/2019 Graphs ADT

    45/65

    Definitions

    • Undirected graphs:

     – An edge is not an ordered pair but a set of 2vertices.

    •Directed Graph/Digraph: – D = (V, E), such that:

    • D = set of vertices.

    • E = ordered pair of vertices.

     – An edge (u, v) therefore has a direction.• u is the head of the edge.

    • v is the tail.

    45

  • 8/15/2019 Graphs ADT

    46/65

    Directed Acyclic Graphs

    • Directed and has no cycles.

    • Note: a tree is a DAG but not all DAGs are

    trees.

    46

  • 8/15/2019 Graphs ADT

    47/65

    Undirected Graphs

    • Vertices are connected by undirected arcs:

     – No arrow.

     – No head or tail.

     – An edge is a set not an ordered pair.

     –{v,w} = {w,v}.

    • E.g. V={a,b,c,d}, E={{a,b}, {a,c},{b,c},{c,d}}

    47

  • 8/15/2019 Graphs ADT

    48/65

    Labelled and Weighted Graphs

    • An annotation on edges or vertices.

    48

  • 8/15/2019 Graphs ADT

    49/65

    Labelled and Weighted Graphs

    • To represent an FSM.

    • Geographic info – e.g. distance between cities

    (e.g. TSP).

    • Cost.

    49

  • 8/15/2019 Graphs ADT

    50/65

    Basics

    • Complete Graph:

     – Simple graph (no loops).

     – Any pair of vertices is connected by an edge.

     – G complete graph with n vertices has n(n-1)/2 edges.

    • Planar Graph:

     – Can be drawn on a plane without any edges

    intersecting. – Applications:

    • Wiring microprocessor circuits.

    50

  • 8/15/2019 Graphs ADT

    51/65

    Graph Representation

    • Note that in a directed graph G=(V,E),

    • We can have |V|2 possible edges:

     – 1 vertex = 1 loop.

     – 2 vertices = 1 loop, 1 normal, 1 normal, 1 loop.

     – … 

    51

  • 8/15/2019 Graphs ADT

    52/65

    Graph Representation

    • Adjacency Matrix:

    • Consider G=(V,E), V=(v1,v2,…vn).

    • We can use a n by n matrix A of 1s and 0s where:

    • Ai,j = 1 if (vi,v j) E,

    0 otherwise.

    52

  • 8/15/2019 Graphs ADT

    53/65

    Graph Representation

    • Common representations are the adjacency list and theadjacency matrix.

    • Suppose the vertices of a graph are labeled withnumbers from 0 to (n-1).

    •Adjacency matrix: – An adjacency matrix would then be a 2D array (n by n) of

    Boolean values.

     – A[i, j] true if there is an edge from i to j.

     – If the graph is weighted, then a value in the matrix is the

    weight. – If the graph is undirected A[i, j] = A[j, i]. This will make the

    matrix symmetric about the diagonal.

    53

  • 8/15/2019 Graphs ADT

    54/65

    Graph Representation

    • In adjacency matrices, space is O(|V|2).

    • Irrespective of the number of edges in the

    graph.

    • If |E|

  • 8/15/2019 Graphs ADT

    55/65

    Dense/Sparse Graphs

    • Dense: number of edges is close to the

    maximum number of edges.

    • A complete graph is the most dense.

    • A sparse graph is the opposite.

    • Which implementation is best for a sparse

    graph:

     – Hint: An adjacency matrix always has n2 elements.

    55

  • 8/15/2019 Graphs ADT

    56/65

    Graph Representation

    • Adjacency list:

     – Linked list of linked lists.

     – For a graph with n vertices, the primary linked list

    has n elements. I.e. each vertex is a linked list ofadjacent vertices.

     – Example:A  B  C

    B  C

    C

    D  C

    56

  • 8/15/2019 Graphs ADT

    57/65

    Suitability

    • Consider operation of finding if there is an edge between twovertices v1 and v2.

     – In adjacency matrix:• Examine value of G[V1, V2].

     – In adjacency list:• Locate V1 in primary list, look for V2 in secondary list.

     – Winner is: adjacency matrix.

    • Find all vertices adjacent to a vertex V1 in a graph with n vertices.

     – In adjacency matrix:• Go to the V1 row.

    • Loop n times and scan each column.

     – In adjacency list:• Go the V1 element.

    • The secondary list is the list of adjacent vertices.

     – Winner is: adjacency list.

    57

  • 8/15/2019 Graphs ADT

    58/65

    Topological Sort

    • Major application is job scheduling.

    • Consider example from:

    https://sys.cs.rice.edu/course/comp314/07/le

    ctures/11-topological-sort/

    58

  • 8/15/2019 Graphs ADT

    59/65

    Topological Sort

    • Consider a DAG (directed, acyclic).• A natural ordering.

    • Consider the graph: – A

    •    B

    •   D

     – B•    C

    •    E

     – C

     – D•    E

     – E

    • Intuitively we see that A comes before B. B comes before C.

    • Any vertex comes before those to which it has outbound vertices.

    59

  • 8/15/2019 Graphs ADT

    60/65

    Algorithm

    • Repeat:

     – Find a vertex with no successor.

     – Remove it from graph.

     – Put it at beginning of list.

    • Until graph is empty.

    60

  • 8/15/2019 Graphs ADT

    61/65

    Many Results (from Wikipedia)

    61

  • 8/15/2019 Graphs ADT

    62/65

    Graph Traversals

    • Depth first: – Like depth first for a tree.

     – But a tree specifies a root and a graph does not. Inother words we have to specify a starting vertex.

    • Visit vertex and recursively visit adjacent vertices.

    • Problem is cycles and we must visit every nodeonly once + infinite loop.

     – Keep track of nodes visited.• DFS can be used to determine if graph is

    connected.

    62

  • 8/15/2019 Graphs ADT

    63/65

    Algorithm

    -- Global

    bool[] visited = new bool[number of vertices]Set all visited to false.

    DepthFirst(vertex v)

    {Visit v

    visited[v.number] = true

    for each successor s of v{

    if (visited[v.number] = false){

    DepthFirst(s)

    }}

    }

    63

  • 8/15/2019 Graphs ADT

    64/65

    Breadth First Search

    • Start from v.

    • Put v on a queue.

    • Repeat until queue is empty:

     – Remove vertex at head of queue and call it w.

     – Visit w.

     – For each vertex z emanating from w

    • If z has never been enqueued, enqueue it.

    64

  • 8/15/2019 Graphs ADT

    65/65

    Breadth First Search